使用fwrite将链接列表中的数据写入二进制文件。

我不知道如何处理这项任务。

我特别要使用fwrite,但我的问题是我不知道如何将不同的文件类型(char [],int,double)写入二进制文件中的单行,递增它,然后写下一批数据从链表中增加它…..等等。

下面的代码将链接列表(Data)中的数据写入文本文件,转到下一行并重复该过程,直到列表的最后一个元素写入文本文件。

我需要编写一个具有相同原理的函数,但是使用fwrite将链表中的数据写入二进制文件。 任何想法的家伙?

void WriteTextFile(ListDataType *Data, int numEl) { FILE *textfile; char name[50]; printf("\n\nWhat would you like to call the text file in which the data will be stored?\nNB remember to add \".txt\" after your chosen name!!!\nENTER NAME NOW>>"); scanf("%s", name); textfile = fopen(name, "w+"); do { fprintf(textfile, "%s %d %d %.2lf %.2lf %.2lf %.2lf\n", Data->component.name, Data->component.int1, Data->component.int2, Data->component.double1, Data->double2, Data->double3, Data->double4 ); Data = Data->nextPtr; }while(Data != NULL); fclose(textfile); } 

由于您有一项可变长度数据(动态分配的字符串指针),您必须决定如何在二进制文件中管理它。 您可以在输出结构中为名称留出固定长度的空间。 或者你可以编写一个以零结尾的字符串并以这种方式管理它。 我将选择第二种情况,因为它更符合动态字符串。

这有点冗长,因为问题陈述中没有给出结构的性质。 因此,以下将以任意顺序使用结构元素。

 void WriteBinaryFile(ListDataType *Data, int numEl) { FILE *bin_file; char name[50]; printf("\n\nWhat would you like to call the binary file in which the data will be stored?\n" printf("NB remember to add \".txt\" after your chosen name!!!\nENTER NAME NOW>>"); scanf("%s", name); bin_file = fopen(name, "wb+"); do { //NOTE: include terminating zero in the string output; assumes single-byte chars fwrite(Data->component.name, strlen(Data->component.name)+1, 1, bin_file); fwrite(&Data->component.int1, sizeof(Data->component.int1), 1, bin_file); fwrite(&Data->component.int2, sizeof(Data->component.int2), 1, bin_file); fwrite(&Data->component.double1, sizeof(Data->component.double1), 1, bin_file); fwrite(&Data->component.double2, sizeof(Data->component.double2), 1, bin_file); fwrite(&Data->component.double3, sizeof(Data->component.double3), 1, bin_file); fwrite(&Data->component.double4, sizeof(Data->component.double4), 1, bin_file); Data = Data->nextPtr; } while(Data != NULL); fclose(bin_file); } 

如果程序需要将其读回,则首先读取名称并读取字符,直到找到0 。 然后它将根据其数据类型读取其他元素。 对于每个写入的结构,将重复该过程。 但这不一定是便携式的。 你必须要小心int时间的大小等。

你想要做的事情如下:

 fwrite (Data , sizeof(ListDataType), 1, fileHandle); 

这将从数据点开始写入sizeof(ListDataType)字节。 如果您的Data对象中有任何指针 – 您需要定义一个新结构,将值复制到该结构,然后编写新结构。

如果你不能为动态字符串定义最大长度,那么你可以定义一个包含字符串长度的结构 – 写结构,然后是字符串 – 这样你就会知道要读回多少字节。 无论哪种方式,struct都是读取和写入bin文件的最佳方式。

好的 – 带例子的编辑……

如果您定义组件结构,如:

  typedef struct Component_Struct { int int1; int int2; double double1; double double2; double double3; double double4; int nameLen; char* name; } COMPONENT, *LPCOMPONENT; 

请注意额外的nameLen – 在将值分配给结构时填充此值。

然后在你的写函数中:

  FILE * pFile; pFile = fopen ("myfile.bin", "wb"); do { fwrite(Data->Component, sizeof(COMPONENT)-sizeof(char*), 1, pFile) fwrite(Data->Component.name, sizeof(char), Data->Component.nameLen, pFile) Data = Data->nextPtr; } while(Data !=NULL); fclose (pFile); 

您应该以不同的方式编写不同的组件,检查样

 fwrite(Data->stringcomponent, strlen(Data->stringcomponent)+1, 1, binaryfile); // writes 1 string fwrite(&Data->doublecomponent, sizeof(double), 1, binaryfile); // writes 1 double fwrite(&Data->intcomponent, sizeof(int), 1, binaryfile); // writes 1 int 

所以,不是单个fprintf,你将有多个不同数据的fwrites,注意不同种类的长度计算(strlen + 1或sizeof)以及是否必须添加&