#include<iostream>
#include<iomanip>
#include<fstream>
using namespace std;
int main(){
FILE *fp;
fp=fopen("in.txt","r");
ifstream fin;
fin.open("in.txt");
ofstream fout;
fout.open("out.txt");
int a,b;
fscanf(fp,"%d",&a);
fin >> b;
return 0;
}
测试结果:
in.txt中 54 89
而调试可知
a 54
b 54
猜测:fp与ifstream对应源文件的两个副本,一个文件形式,一个流,不同于scanf cin 中对应于同一个控制台。故,fscanf与fin相互独立,不可像scanf cin那样使用。
本文探讨了C++中使用FILE指针与ifstream进行文件读取的区别,通过实例对比了fscanf与fin操作符的行为,指出两者分别对应文件和流的不同形式,强调在C++中它们独立工作,不能像C语言中的scanf与cin那样交互使用。

482

被折叠的 条评论
为什么被折叠?



