c语言关于文件保存的问题
文件操作保存后重新运行就没有了,打印不出来,而且之前保存之后再打印会打印两次,保存的文件可以找到,但是就是打印不出来,而且用绝对路径表示的话就会报错
void savestudent()
{
//保存到文件中,首先打开文件
FILE* pf = fopen("stuinfo.txt", "w");//以写(write)的方式打开文件
if (pf == NULL)
{
printf("打开文件失败.\n");
printf("%s\n", strerror(errno));//错因
system("pause");//暂停,按一下回车继续运行
system("cls");//清屏
return;//可不写
}
//遍历链表
Node* p = g_pHead;
while (p != NULL)
{
fwrite(&p->stu, 1, sizeof(student), pf);
p = p->pNext;//节点连接起来
}
//关闭文件
fclose(pf);
printf("\n数据保存成功\n");
system("pause");//暂停,按一下回车继续运行
system("cls");//清屏
}
不要把
fopen("...","...");fscanf,fprintf,fgets,fgetc,fputc,fclose //读时把\r\n替换成\n,写时把\n替换成\r\n;读到\x1a就设置EOF;读写的内容当字符看待
和
fopen("...","...b");fseek,ftell,fread,fwrite,fscanf,fprintf,fgets,fgetc,fputc,fclose //不作以上替换,遇到\x1a仍继续读;读写的内容当字节看待
弄混了
绝对路径中的\要用\\代替