文件输出不正确
Incorrect output from file
我正在尝试将一些数据输入到一个文件中,然后在控制台上读取并打印它。
这是我的代码:
fp=fopen("user1","w+");
char line[1000];
char *c;
//write on file
while(1)
{
fgets(line,sizeof(line),stdin);
if(strcmp(line,":wq\n")==0) //input ends when user enters :wq
break;
else
fputs(line,fp);
}
//read from file
fflush(stdin);
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
else
printf("%ch",c);
}
我输入数据为
bjsa
:wq
输出:
h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h
有什么问题?
我看到两个问题
1) 您的 fgetc
分配给一个字符点。那是错的。它应该是一个 int
2) 写入文件后,应先关闭再打开读取
类似于:
fp=fopen("user1","w+");
char line[1000];
int c; // notice ......
//write on file
while(1)
{
fgets(line,sizeof(line),stdin);
if(strcmp(line,":wq\n")==0) //input ends when user enters :wq
break;
fputs(line,fp);
}
fclose(fp);
//read from file
fp=fopen("user1","r");
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
printf("%ch",c);
}
fclose(fp);
我正在尝试将一些数据输入到一个文件中,然后在控制台上读取并打印它。
这是我的代码:
fp=fopen("user1","w+");
char line[1000];
char *c;
//write on file
while(1)
{
fgets(line,sizeof(line),stdin);
if(strcmp(line,":wq\n")==0) //input ends when user enters :wq
break;
else
fputs(line,fp);
}
//read from file
fflush(stdin);
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
else
printf("%ch",c);
}
我输入数据为
bjsa
:wq
输出:
h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h h
有什么问题?
我看到两个问题
1) 您的 fgetc
分配给一个字符点。那是错的。它应该是一个 int
2) 写入文件后,应先关闭再打开读取
类似于:
fp=fopen("user1","w+");
char line[1000];
int c; // notice ......
//write on file
while(1)
{
fgets(line,sizeof(line),stdin);
if(strcmp(line,":wq\n")==0) //input ends when user enters :wq
break;
fputs(line,fp);
}
fclose(fp);
//read from file
fp=fopen("user1","r");
while(1)
{
c=fgetc(fp);
if(c==EOF)
break;
printf("%ch",c);
}
fclose(fp);