这是我的代码:
#include<stdio.h>
#include<conio.h>
#include<string.h>
int main()
{
FILE *p;char c[79];
clrscr();
p = fopen("file1.dat","w");
printf("\nenter lines and enter end1 to end ");
scanf("%s",c);
if (strcmp(c,"end1") != 0)
do
{
fputc('\n',p);
fputs(c,p);
gets(c);
} while(strcmp(c,"end1")!=0);
fclose(p);
p = fopen("file1.dat","r");
printf("lines in file:\n");
while(!feof(p))
{
fgets(c,80,p);
printf("%s\n",c);
}
fclose(p);
return 0;
getch();
}
我的问题是当我输入(并写入文件)时
hello
my name is abc
然后键入 end1 终止,当文件内容被读取和打印时,我得到的输出为
hello
my name is abc
为什么打印两个换行符而不是 1 个以及如何解决这个问题?