[英]Store data from binary file into dynamic array of strings
I have a binary file where I store numbers as strings in this fashion: 11 43 89 101 etc 我有一个二进制文件,其中我以这种方式将数字存储为字符串:11 43 89 101等
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. 它总是存储数字及其后的
'\\0'
。
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);
警告:指针转换不兼容的整数,将'char'传递给'void *'类型的参数[-Wint-conversion] read(fd,positions [i] [j],1);
About positions[i][j]
. 关于
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
malloc: *对象0x20400036的错误:未分配要重新分配的指针*在malloc_error_break中设置断点以调试中止陷阱:6
I really think I am supposed to realloc every time cause all_names value gets updated at an earlier part of my code. 我真的认为我应该每次在代码的较早部分更新all_names值时就重新分配。 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]
. read()
的第二个参数需要是指向数组元素的指针,请使用&positions[i][j]
。
You have to allocate memory for all the strings, you're just allocating one element of positions
. 您必须为所有字符串分配内存,而只是分配
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
. 您还需要将
all_names
设置为文件中的实际字符串数,而不是0
。 I'm not sure where you get that from. 我不确定你从哪里得到的。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.