《编写一个程序,从一个文件中读出字符串,并显示在屏幕上》

注意:在程序的第11行用fgets函数读入字符串时,指定一次读入10个字符,但按fgets函数的规定,

  如果遇到“ ”就结束字符串输入,“ ”作为最后一个字符也读入到字符数组中

//编写一个程序,从f:\FILE_1\file_2.txt中读回字符串

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
int main()
{
FILE *fp;
char str[3][10];
int i=0;
if((fp=fopen("f:\FILE_1\file_2.txt","r"))==NULL)
{
printf("can't open file! ");
exit(0);
}
while(fgets(str[i],10,fp)!=NULL)
{
printf("%s",str[i]);
i++;
}
fclose(fp);
return 0;

}

原文地址:https://www.cnblogs.com/sun-/p/4811102.html