c语言读文件程序

来源:百度知道 编辑:UC知道 时间:2024/05/13 07:42:27
文件中为
1 2 3 4 5 6
a b c d e f
要求逐行读写 一次 只能读一行 并输出
请问应该怎么写呢?
要说的是 只能读一行 就是读一次str的内容是1 2 3 4 5 6在读一次才能使a b c d e f

现在不就是一次读一行了,难道不是吗?如果你要自己手动一行一行地读取,那就自己改下代码,写得这么明白了,我也不想再改了。
#include<stdio.h>

int main()
{
FILE *fp;
char str[20];

if((fp=fopen("c:\\abc.txt","r"))==NULL)
{
printf("can't open file.\n");
exit(1);
}
while(fgets(str,50,fp)!=NULL)
{
printf("%s\n",str);
}
fclose(fp);
getch();
return 0;
}