gpt4 book ai didi

c - 如何解决C中读取/写入二进制文件时的段错误

转载 作者:行者123 更新时间:2023-11-30 17:26:59 25 4
gpt4 key购买 nike

这是我尝试写入副本并从二进制文件读取的结构定义

   typedef struct carType Car;
struct carType {
int vehicleID;
char make[20];
char model[20];
int year;
int mileage;
double cost;
Car *next;
};

这是我写入二进制文件的代码(汽车状态)

   void writeBinFile(Car *headPointer)
{
char fileName[20];
//prompt user for name of textfile to print to
scanf("%s", fileName);
FILE *ftp;
Car *start = headPointer->next;
ftp = fopen(fileName, "wb");
char separator = '0';
while(start != NULL)
{
//write out 1 cell of data, cell contains 4 bytes
fwrite(&start->year,sizeof(int), 1, ftp);
fwrite(start->make,sizeof(char), strlen(start->make), ftp);
fwrite(&separator, sizeof(char), 1, ftp);
fwrite(start->model, sizeof(char), strlen(start->make), ftp);
fwrite(&separator, sizeof(char), 1, ftp);
fwrite(&start->cost, sizeof(float), 1, ftp);
fwrite(&start->mileage, sizeof(float),1,ftp);
fwrite(&start->vehicleID, sizeof(int), 1, ftp);
start = start->next;
}
fclose(ftp);
}

这是我从二进制文件读取的代码(到汽车的状态)

    void readFromBinFile(Car *headPointer)
{
char fileName[20];
//prompt user for name of textfile to print to
scanf("%s", fileName);
FILE *ftp;
Car *previous = headPointer;
ftp = fopen(fileName, "rb");
Car *current;
//go until the end of file is reached
while(!feof(ftp))
{
current = (Car *)malloc(sizeof(Car));
previous->next = current;
//program receives 1 cell, that cell contains 4 bytes
fread(&current->year, sizeof(int),1,ftp);
printf("%d\n",current->year);
char make[25];
int count = 0;
char oneAtATime= 'a';
while(oneAtATime != '0')
{
fread(&oneAtATime, sizeof(char),1,ftp);
if(oneAtATime!='0')
{
make[count] = oneAtATime;
count++;
}
}
make[count] = 0;
strcpy(current->make, make);
char model[25];
count = 0;
oneAtATime= 'a';
while(oneAtATime != '0')
{
fread(&oneAtATime, sizeof(char),1,ftp);
if(oneAtATime!='0')
{
model[count] = oneAtATime;
count++;
}
}
model[count] = 0;
strcpy(current->model, model);
fread(&current->cost, sizeof(float),1, ftp);
fread(&current->mileage, sizeof(int),1,ftp);
fread(&current->vehicleID, sizeof(int),1,ftp);
previous = previous->next;
}
fclose(ftp);
}

上次我因未向新车分配内存而出现段错误 Why am I getting a segmentation failure? 。这次我一定要这么做。我查了这个Segmentation fault when reading a binary file into a structureSegmentation fault while reading binary file in C但我的字段是值,而不是指针。有人看到一个明显的问题吗?每当我尝试运行此程序时,我都无法测试任何内容,我收到该错误。问题似乎是读取,但我不确定写入中的某些代码是否导致读取失败

最佳答案

'0' 不是空终止符。 0 或 '\0' 是(请注意第一个缺少引号,第二个缺少转义字符)。 '0' 的值为 48,而不是零。

这些是有效的选项。

char separator = 0;

char separator = '\0';

您有一个错误:

fwrite(start->model, sizeof(char), strlen(start->make), ftp);   // 'make' size used to write 'model'

其次,您可以简化代码,而不是将分隔符 null 作为单独的步骤编写,只需写出完整的字符串,包括 null 终止符。

fwrite(start->make, 1, strlen(start->make) + 1, ftp);

但是,您打算如何读回字符串?您将使用什么函数调用来读取二进制字符串(可能是可变长度且带有空终止符)?更好的方法是使用 sizeof 而不是 strlen 来写入填充缓冲区。

fwrite(start->make, 1, sizeof(start->make), ftp);

但是,即使这样也很脆弱,因为如果您的结构成员从固定字符数组更改为字符串(指针),则 sizeof() 将默默返回不同的值。使用常量会更安全。

const int SMALL_STRING_LEN = 20;
const int MAKE_LEN = SMALL_STRING_LEN;
const int MODEL_LEN = SMALL_STRING_LEN;

char make[MAKE_LEN];
char make[MODEL_LEN];

fwrite(start->model, 1, MODEL_LEN, ftp);
fwrite(start->make, 1, MAKE_LEN, ftp);

关于c - 如何解决C中读取/写入二进制文件时的段错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26568422/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com