阅读:1148回复:2
20分请教TC20下面读写文件时出现的开启文件失败问题,大家有无类似经历?
操作文件时出现奇怪问题,大家有无类似经历?
用的是TC2.0,程序反复打开文件,第一轮读写没有问题,第二轮写没有问题,读的时候不能打开文件,分析半天都找不到原因。大虾们帮忙看看。 #include <conio.h> #include <stdio.h> #define File_IO \"D:\\\\tc20\\\\work\\\\output\\\\SysSet.txt\" char Result = 0; char *Str = \"Hello!\"; void SaveSub(void) { FILE *FilePtr = NULL; FilePtr = fopen(File_IO,\"r+\"); if(FilePtr==NULL) { printf(\"Creat file Error!\\n\"); exit(0); } else { Result = fseek(FilePtr,0,SEEK_SET); Result = fprintf(FilePtr,\"%s\\n\",Str); Result = printf(\"Save: %s\\n\",Str); } Result = fclose(FilePtr); } void ReadSetting(void) { FILE *FilePtr = NULL; char *ReadStr=\"\"; FilePtr = NULL; FilePtr =fopen(File_IO,\"r\"); /* 第二次执行到这里时出错,不能打开文件 */ if(FilePtr == NULL) { printf(\"can not open file ABC.txt for reading\\n\"); exit(0); } else { Result = fseek(FilePtr,0,SEEK_SET); Result = fscanf(FilePtr,\"%s\\n\",ReadStr); printf(\"\\tRead: %s\",ReadStr); } Result = fclose(FilePtr); if(Result != 0) { printf(\"close file Error!\\n\"); }; } void main(void) { SaveSub(); ReadSetting(); SaveSub(); ReadSetting(); SaveSub(); ReadSetting(); SaveSub(); ReadSetting(); SaveSub(); while(!kbhit()); } [编辑 - 6/17/03 by study51] [编辑 - 6/17/03 by study51] |
|
沙发#
发布于:2003-06-17 10:30
char *ReadStr=\"\";==> char ReadStr[100]
ReadStr没分配内存!! |
|
板凳#
发布于:2003-06-17 10:50
果然,谢谢
|
|