用fputs函数写文件(C语言)

用fputs函数把10个字符串输出到文件中,再从文件中读取这10个字符串到一个数组中,最后把这个数组输出到屏幕上。
To Beating_marks -我说的是10个字符串,而不是含10个字符的字符,而且你写的与我的要求不符

从string文件中读入一个含10个字符的字符串。
#include<stdio.h>
main()
{
FILE *fp;
char str[11];
if((fp=fopen("d:\\jrzh\\example\\string","rt"))==NULL)
{
printf("\nCannot open file strike any key exit!");
getch();
exit(1);
}
fgets(str,11,fp);
printf("\n%s\n",str);
fclose(fp);
}

在建立的文件string中追加一个字符串。
#include<stdio.h>
main()
{
FILE *fp;
char ch,st[20];
if((fp=fopen("string","at+"))==NULL)
{
printf("Cannot open file strike any key exit!");
getch();
exit(1);
}
printf("input a string:\n");
scanf("%s",st);
fputs(st,fp);
rewind(fp);
ch=fgetc(fp);
while(ch!=EOF)
{
putchar(ch);
ch=fgetc(fp);
}
printf("\n");
fclose(fp);
}
温馨提示:答案为网友推荐,仅供参考
相似回答