C语言:读写TXT
fopen() 改为: if((fp=fopen("1s.txt","w+"))==NULL)
fputc(p,fp); 改为:fprintf(fp,"%d",p);
读语句前,加一句文件回绕到文件头: rewind(fp);
#include <stdio.h> main() { FILE *fp; char read[1000]; char s; long p; if((fp=fopen("1s.txt","w+"))==NULL) { printf("\nOpen file error!press any key exit!"); getchar(); //exit(0); } p=123457; s='\n'; fprintf(fp,"%d",p); fputc(s,fp); fprintf(fp,"%d",p); rewind(fp); fgets(read,1000,fp); printf("%s",read); system("pause"); return 0; }