为什么下面的代码不起作用?我的意思是,它在控制台输出上显示各种奇怪的字符。为什么我需要刷新我的I/O流才能获得正确的结果?
#include <stdio.h>
char mybuffer[80];
int main()
{
FILE * pFile;
pFile = fopen ("example.txt","r+");
if (pFile == NULL) perror ("Error opening file");
else {
fputs ("test",pFile);
fgets (mybuffer,80,pFile);
puts (mybuffer);
fclose (pFile);
return 0;
}
}
但是,下面的代码运行良好。
#include <stdio.h>
char mybuffer[80];
int main()
{
FILE * pFile;
pFile = fopen ("example.txt","r+");
if (pFile == NULL) perror ("Error opening file");
else {
fputs ("test",pFile);
fflush (pFile);
fgets (mybuffer,80,pFile);
puts (mybuffer);
fclose (pFile);
return 0;
}
}
为什么我需要刷新流以获得正确的结果?
Thx很多!!!!!! – Jaebum 2010-02-09 16:47:05
但为什么第二个来源的输出不是“测试”? 它只是没有显示任何内容。 – Jaebum 2010-02-09 17:04:15
@Lee:简单的说,你从你写作的地方开始阅读,你需要回到开始工作。 – falstro 2010-02-09 17:22:23