从文件string.dat中读回字符串,并在屏幕上显示
- 假设我现在D盘中有一个string.dat的文件,文件里面有内容,现在我将文件里面的内容读出,显示到屏幕上。
- 代码如下:
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fp;
int i=0;
char a[3][10];
while((fp=fopen("D:\\string.dat","r"))==NULL){
printf("cannot open file");
exit(0);
}
while(fgets(a[i],8,fp)!=NULL){
printf("%s",a[i]);
i++;
}
fclose(fp);
return 0;
}
- 运行结果如图: