【发布时间】:2017-10-18 14:33:37
【问题描述】:
您好,我有一个包含几个变量的结构,我尝试从/向二进制文件写入和读取它们,但是当我读取它们时,我只看到奇怪的符号而且我不知道我搞砸了什么,我尝试了几个变体但它们都不起作用
typedef struct catalog
{
char num[20];
char name[80];
char author[50];
double price;
int year;
char publisher[80];
} Catalog;
Catalog* createCatalogData()
{
Catalog* c = malloc(sizeof(*c));
if (!c)
{
// Ups... add error handling
exit(0);
}
printf("Add num ");
getString(&c->num);
printf("Add name ");
getString(&c->name);
printf("add author ");
getString(&c->author);
printf("Add price ");
if (scanf("%lf", &c->price) != 1)
{
// Ups... add error handling
exit(0);
}
printf("Add publisher");
getString(&c->publisher);
printf("Add year");
if (scanf("%d", &c->year) != 1)
{
// Ups... add error handling
exit(0);
}
char *filePath = malloc(strlen(c->num) + 13);
char *folderName = "Catalogs\\";
strcpy(filePath, folderName);
strcat(filePath, c->num);
strcat(filePath, ".bin");
FILE *file = fopen(filePath, "wb");
if (file == NULL)
{
printf("Error opening file!\n");
exit(1);
}
fwrite(&c->num,1, strlen(c->num), file);
fwrite(&c->name,1, strlen(c->name), file);
fwrite(&c->author,1, strlen(c->author), file);
fwrite(&c->price, 1, sizeof(double), file);
fwrite(&c->publisher,1, strlen(c->publisher), file);
fwrite(&c->year,1, sizeof(int), file);
fclose(file);
return c;
}
Catalog* readCatalogData(char *filePath)
{
Catalog* c = malloc(sizeof(*c));
FILE* fh;
fopen_s(&fh, filePath, "rb");
//check if file exists
char *ptr;
//read line by line
const size_t line_size = 300;
char* line = malloc(line_size);
int counter = 0;
char* date;
fread(c->num, 1, 21, fh);
fread(c->name, 1, 80, fh);
fread(c->author, 1, 50, fh);
fread(&c->price, 1, sizeof(double), fh);
fread(c->publisher, 1, 80, fh);
fread(c->year, 1, sizeof(int), fh);
return c;
}
【问题讨论】:
-
写作时不要在
&c->num等中使用&。 -
我已经改变了它,但仍然无法正常工作,但如果价格没有 &
-
malloc(strlen(c->num) + 13)off-by-one 错误。 -
我强烈建议您阅读指针以及如何/何时将数组视为指针。
-
你考虑过使用JSON吗?你不会有二进制(但文本)文件,你可以很容易地找到好的库来解析它们。