文件系统:读取,文件不存在则创建
1 #include<stdio.h> 2 #include<stdlib.h> 3 #define WAY "save.txt" 4 int main(int argc,int argv[]) 5 { 6 FILE *fp_read,*fp_write; 7 8 fp_read=fopen(WAY,"r+"); 9 10 if(fp_read==NULL) 11 { 12 puts("Not exist,create now"); 13 fp_write=fopen(WAY,"w+"); 14 if(fp_write==NULL) 15 puts("Create failed"); 16 else 17 { 18 puts("Create succeed"); 19 fputs("good\n",fp_write); //写入文件:成功时返回非负值, 失败时返回EOF 20 fflush(fp_write); //将缓存区内的数据存入文档 21 fp_read=fp_write; 22 fseek(fp_write, 0, SEEK_SET); //将光标移动到文件结尾:SEEK_END,文件开头:SEEK_SET 23 24 } 25 } 26 else 27 puts("Open succeed"); 28 29 //打印文件内容 30 rewind(fp_read); //将光标移动到文件开头 31 int ch = 0; 32 while((ch=fgetc(fp_read))!=EOF) 33 putchar(ch); 34 35 system("pause"); 36 37 38 }