简体   繁体   English

将数据从二进制文件存储到字符串的动态数组中

[英]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.

相关问题 优雅的方式将文本文件数据存储在动态字符串数组中 - Elegant way to store text file data in a dynamic array of strings 如何将文件中的单词存储到动态字符串数组中? - how to store words from a file to a dynamic array of strings? 如何从C中的动态数组中的二进制文件读取数据 - How to read data from binary file in dynamic array in C 如何从文本文件中分离二进制字符串并将其存储在1d或2d char数组中? - How to separate binary strings from text file and store them in 1d or 2d char array? 使用动态 memory 分配将文件中的字符串存储在二维数组中 - Store in a two-dimensional array strings from a file using dynamic memory allocation 将文件中的字符串存储到动态分配的数组中 - Store strings from file into dynamically alocated array 从文件读取并将字符串存储到C语言中的多维字符串数组中 - Read From File and Store Strings into a Multidimensional Array of Strings in C 如何从文本文件读取二进制数据并将其存储在C中的2D数组中 - How do I read binary data from a text file and store it in a 2D array in C 尝试将二进制文件中存储的数据打印到动态数组中时出现段错误 - Seg fault while trying to print data stored from binary file into dynamic array 读取二进制文件并将所有数据存储到结构数组中 - Read a binary file and store all data into array of structures
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM