c
#include<stdio.h>#include<stdlib.h>#include<string.h>void main(){ char str[10]; FILE *fp; if((fp=fopen("123.txt","wr"))==NULL) { printf("cann't open file\n"); exit(0); } do { printf("please enter a string:"); gets(str); strcat(str,"\n"); fputs(str,fp); } while(*str!='\n'); // rewind(fp); while(!feof(fp)) { fgets(str,9,fp); printf("%s",str); } fclose(fp);}
#include<stdio.h>#include<stdlib.h>#include<string.h>int main(){ char str[10]; FILE *fp; if((fp=fopen("123.txt","w+"))==NULL)//就修改了这里,用w+创建并读写 { printf("cann't open file\n"); system("pause"); exit(0); } do { printf("please enter a string:"); gets(str); strcat(str,"\n"); fputs(str,fp); } while(*str!='\n'); // rewind(fp); while(!feof(fp)) { fgets(str,9,fp); printf("%s",str); } fclose(fp); system("pause"); return 0;}