printf ( " 输入字符串: \ n " ) ; fgets ( sentence , ( sizeof sentence / sizeof sentence [ 0 ] ) , stdin ) ; fprintf ( fptr , " %s " , sentence ) ; fclose ( fptr ) ; return 0 ;

输出结果为:

输入字符串: runoob.com

打开文件 runoob.txt:

$ cat runoob.txt runoob.com

C 语言实例 C 语言实例

FILE *fp; /*文件指针*/ int len; /*行字符个数*/ if((fp = fopen("test.txt","r")) == NULL) perror("fail to read"); exit (1) ; while(fgets(buf,MAX_LINE,fp) != NULL) len = strlen(buf); buf[len-1] = '\0'; /*去掉换行符*/ printf("%s %d \n",buf,len - 1); return 0;
big_tree