fscanf函数读入错误
fscanf函数读入异常
文件中已读入“8 9”,但程序输出为50 4534376,这是怎么回事?
------解决方案--------------------
你写入数据后没关闭文件,文件指针在你写入数据的后面,你再去读文件,当然会出错
你写入文件后先关闭,再去读,或者 fseek(file,0,SEEK_SET)在去读
------解决方案--------------------
哈哈,如楼上所示啊
------解决方案--------------------
#include <stdio.h>
int main()
{
FILE* file;
int a,b;
file = fopen( "test.txt", "w+");
fprintf (file, "%d %d",8,9);
fscanf (file, "%d", &a);
fscanf (file, "%d", &b);
printf ("%d %d", a, b);
fclose (file);
return 0;
}
文件中已读入“8 9”,但程序输出为50 4534376,这是怎么回事?
fscanf
------解决方案--------------------
你写入数据后没关闭文件,文件指针在你写入数据的后面,你再去读文件,当然会出错
你写入文件后先关闭,再去读,或者 fseek(file,0,SEEK_SET)在去读
------解决方案--------------------
#include <stdio.h>
int main()
{
FILE* file;
int a,b;
file = fopen( "test.txt", "w+");
fprintf (file, "%d %d",8,9);
fseek(file,0,SEEK_SET); //这个文件指针置零。
fscanf (file, "%d", &a);
fscanf (file, "%d", &b);
printf ("%d %d", a, b);
fclose (file);
return 0;
}
哈哈,如楼上所示啊
------解决方案--------------------