将数据从二进制文件存储到字符串的动态数组中 [英] Store data from binary file into dynamic array of strings
问题描述
我有一个二进制文件,其中我以这种方式将数字存储为字符串:11 43 89 101等
我想只使用系统命令来读取存储的数字并将它们存储在字符串动态数组中,因为我不知道字符串最终将持续多长时间或多少.这是相关代码:
char **positions;
int all_names=0,i,j;
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
i=0;
j=0;
do{
positions=(char**)malloc(sizeof(char*));
(*positions)[i]=(char*)malloc((MAX_SIZE+1)*sizeof(char));
do{
read(fd,positions[i][j],1);
}while(positions[i][j+1]!='\0');
i++;
}while(i<all_names);
for(i=0; i<all_names; i++){
for(j=0; positions[i][j]!='\0';j++){
printf("%c", positions[i][j]);
}
printf("\n");
}
}
所有名称都跟踪二进制文件中的条目数量.
当我运行它时,我遇到了分段错误.我存储数字的部分工作正常,我已经检查了文件.它总是存储数字和其后的'\0'
.
我得到这个警告,但不知道如何解决
警告:不兼容的整数到指针的转换,将'char'传递给'void *'类型的参数[-Wint-conversion] read(fd,positions [i] [j],1);
关于positions[i][j]
.
感谢您的帮助
将代码更改为:
char **positions;
int all_names=0,i,j;
positions=(char**)malloc(sizeof(char*));
*positions=(char*)malloc((MAX_SIZE+1)*sizeof(char));
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
i=0;
j=0;
for(i=0; i<all_names; i++){
positions=(char**)realloc(*positions,(all_names) * sizeof(char*));
positions[i]=(char*)malloc((all_names+1)*sizeof(char));
for(j=0; ;j++){
read(fd,&positions[i][j],1);
if (positions[i][j] == ' ') {
break;
}
}
}
for(i=0; i<all_names; i++){
printf("%s\n", positions[i]);
}
}
现在我在运行时出现错误:
malloc: *对象0x20400036的错误:未分配正在重新分配的指针 * 在malloc_error_break中设置一个断点以进行调试 中止陷阱:6
我真的认为我应该每次在代码的较早部分更新all_names值时就重新分配.我在做什么错了?
您需要在读取后的(而不是之前)中检查空字节.
read()
的第二个参数必须是指向数组元素的指针,请使用&positions[i][j]
.
您必须为所有字符串分配内存,而只是分配positions
的一个元素.
positions = malloc(all_names * sizeof(char*));
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
for(i=0; i<all_names; i++){
positions[i] = malloc((MAX_SIZE+1)*sizeof(char));
for(j=0; ;j++){
read(fd,&positions[i][j],1);
if (positions[i][j] == '\0') {
break;
}
}
}
for(i=0; i<all_names; i++){
printf("%s\n", positions[i]);
}
您还需要将all_names
设置为文件中的实际字符串数,而不是0
.我不确定你从哪里得到的.
I have a binary file where I store numbers as strings in this fashion: 11 43 89 101 etc
I want, by using only system commands, to read the numbers stored and store them in a string dynamic array, because i don't know how long the strings will end up being or how many. Here is the relevant code:
char **positions;
int all_names=0,i,j;
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
i=0;
j=0;
do{
positions=(char**)malloc(sizeof(char*));
(*positions)[i]=(char*)malloc((MAX_SIZE+1)*sizeof(char));
do{
read(fd,positions[i][j],1);
}while(positions[i][j+1]!='\0');
i++;
}while(i<all_names);
for(i=0; i<all_names; i++){
for(j=0; positions[i][j]!='\0';j++){
printf("%c", positions[i][j]);
}
printf("\n");
}
}
All names keeps track of the amount of entries in the binary file.
When I run it I get a segmentation fault. The part where I store the numbers works fine I have checked the file. It always stores the number and a '\0'
after it.
I get this as warning but don't know how to fix it
warning: incompatible integer to pointer conversion passing 'char' to parameter of type 'void *' [-Wint-conversion] read(fd,positions[i][j],1);
About positions[i][j]
.
Thanks for any help
Edit: changed code to:
char **positions;
int all_names=0,i,j;
positions=(char**)malloc(sizeof(char*));
*positions=(char*)malloc((MAX_SIZE+1)*sizeof(char));
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
i=0;
j=0;
for(i=0; i<all_names; i++){
positions=(char**)realloc(*positions,(all_names) * sizeof(char*));
positions[i]=(char*)malloc((all_names+1)*sizeof(char));
for(j=0; ;j++){
read(fd,&positions[i][j],1);
if (positions[i][j] == ' ') {
break;
}
}
}
for(i=0; i<all_names; i++){
printf("%s\n", positions[i]);
}
}
Now I get an error on runtime:
malloc: * error for object 0x20400036: pointer being realloc'd was not allocated * set a breakpoint in malloc_error_break to debug Abort trap: 6
I really think I am supposed to realloc every time cause all_names value gets updated at an earlier part of my code. What am I doing wrong?
You need to check for the null byte after you read, not before.
The second argument to read()
needs to be a pointer to the array element, use &positions[i][j]
.
You have to allocate memory for all the strings, you're just allocating one element of positions
.
positions = malloc(all_names * sizeof(char*));
fd=open(argv[2],O_RDWR|O_CREAT,S_IRWXU);
for(i=0; i<all_names; i++){
positions[i] = malloc((MAX_SIZE+1)*sizeof(char));
for(j=0; ;j++){
read(fd,&positions[i][j],1);
if (positions[i][j] == '\0') {
break;
}
}
}
for(i=0; i<all_names; i++){
printf("%s\n", positions[i]);
}
You also need to set all_names
to the actual number of strings in the file, not 0
. I'm not sure where you get that from.
这篇关于将数据从二进制文件存储到字符串的动态数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!