我有一个函数问题,它应该将一个结构写入文件,这些文件会被创建,但无论我做什么,它们都会保持空白。
这是我试图写入文件的结构:
typedef struct
{
double dA;
double dA1;
double dB;
double dB1;
double dAwnser;
char cStepOne[24];
char cStepTwo[24];
char cStepThree[20];
char cFormula[26];
} equationData_t;
equationData_t equation;
这是将结构写入文件的函数:
void writeDataToFile(equation)
{
int iSizeOfStruct = 0;
char cFileName[20];
int iQuitProgram = 0;
iSizeOfStruct = sizeof(equationData_t);
while (1)
{
printf("Give file name with extension (Max 20 char.):\n:");
scanf("%s",&cFileName[0]);
pnf = fopen(cFileName, "r+");
if (pnf == NULL)
{
printf ("\nError: File not found!\n1.Try again.\n2.Quit.\n3.Make new file.\n:");
scanf("%d",&iQuitProgram);
switch(iQuitProgram)
{
case 1: break;
case 2: exit(1);
case 3: makeNewFile();
}
}
if (pnf != NULL)
{
printf("\n************************************\nFile opend!\nWriting data to file.\n");
fwrite(equation, iSizeOfStruct, 1, pnf);
fclose(pnf);
printf("Data written");
break;
}
}
}
答案 0 :(得分:1)
您可以尝试使用open()然后使用pwrite()而不是fopen()和fwrite()。
但是,在您的代码中:
fwrite(equation, iSizeOfStruct, 1, pnf);
应修改为:
fwrite(&equation, iSizeOfStruct, 1, pnf);