调用fputs函数,把10个字符串输出到文件中,再从此文件中读入这10个字符串放在一个字符串数组中;最后把字符串数组中的字符串输出到终端屏幕。

源程序:

#include <stdio.h>
#include <stdlib.h>
int main()
{
int i;
char s[100];
FILE *fp;
if((fp=fopen("e:\file.txt","w+"))==NULL)
{
printf("can't open this file. ");
exit(0);
}
for(i=0;i<5;i++)
{
gets(s);
fputs(s,fp);
fprintf(fp," ");
}

rewind(fp); //将文件指针指回到文件开始的地方

for(i=0;i<5;i++)
{
fgets(s,100,fp);
printf("%s",s);
}

fclose(fp);
return 1;
}

运行结果:

原文地址:https://www.cnblogs.com/duanqibo/p/12033288.html