c++读文件
- C/C++ code
#include <iostream>#include <fstream>using namespace std;int main(){ const char * inFile="D:\\in1.txt"; ifstream in(inFile,ifstream::in); int temp; while(!in.eof()) { temp=0; in>>temp; cout<<temp<<endl; } system("pause"); return 0; }代码是这样的
in1.txt中保存的是
7894561
3325645
6492313
1234569
1236548
9875222
3698521
4563219
9517532
9517538
4563219
1
23
85
987
258
159
753
6549823
4563219
7777777
8888888
1234569
3325645
4563219
为什么读的不对啊
[解决办法]
我这里显示正确
[解决办法]
- C/C++ code
//楼主用的什么编译器啊?我用的VC6.0结果正确,你文件的最后不要有回车之类的//另外,楼主程序不太严格,我给你稍微改了下,希望对你有所帮助#include <iostream>#include <fstream>using namespace std;int main(){ const char * inFile="D:\\in1.txt"; ifstream in(inFile,ifstream::in); if (in.peek() == EOF) { cout << "file is empty."<<endl; return 0; } int temp; while(!in.eof()) { temp=0; in>>temp; cout<<temp<<endl; } system("pause"); return 0; }