我正在为学校做作业,我一直让我的fopen函数返回NULL。 请记住,在我需要开始使用动态数组而不是静态定义的数组之前,这个write_info函数运行正常。
以下是我的主要声明:
void main(void)
{
struct assignment *assi = NULL;
char choice;
int choice_check = TRUE;
int loop = TRUE, keep_loop = TRUE;
int nbr_asst = 0;
char asst_file[] = "C:\\Users\\Main\\Documents\\asst.txt";
int nbr_asstfile = 0;
这是函数调用:
write_info(asst_file, assi, nbr_asstfile);
以下是实际功能:
void write_info(char asst_file[], struct assignment *asst_data,
int nbr_asstfile)
{
FILE *fptr;
int count;
fptr = fopen(asst_file, "w");
if (fptr != NULL)
{
fwrite(&nbr_asstfile, sizeof(nbr_asstfile), 1, fptr);
for (count = 0; count < nbr_asstfile; count++)
{
fwrite(&asst_data[count], sizeof(asst_data[count]), 1, fptr);
}
fclose(fptr);
}
else
{
printf("Problem opening the file!");
}
return;
}
如果我只是在从文本文件读入后运行该函数,则重写所有内容都没有问题。但是,如果我在结构中添加一个条目,那么当我尝试编写时,fptr会返回NULL。有谁知道为什么?
我真的希望我以一种人们可以理解的方式询问这一点,如果您需要任何额外信息,请告诉我。我不想发布整个节目,因为它是505行。
提前谢谢大家。