我有一些我想要开始工作的代码.基本上我想打开一个文件并将内容打印到终端.现在,我在.tpp文件中的.tpp文件中只有一个列表(1-10).
int main() {
ifstream inFile;
inFile.open("numbers.txt");
if( inFile.fail()) {
cout<<"Error opening file "<< endl;
return 0;
}
while(!(inFile.fail())) {
int x;
inFile >> x;
cout<<x<< endl;
}
}
Run Code Online (Sandbox Code Playgroud)
这是我到目前为止,它可以打开文件并打印到控制台.唯一的问题是,它打印文件的最后一行两次(因此它打印1-10精细但打印10次两次)我难过自己试图解决这个问题.有任何想法吗?
谢谢你帮我编辑一下!
试试下面的代码
#include <iostream>
#include <fstream>
using namespace std;
int main() {
ifstream inFile;
inFile.open("a.txt");
if( inFile.fail()) {
cout<<"Error opening file "<< endl;
return 0;
}
int x;
while(inFile >> x) {
cout<<x<< endl;
}
}
Run Code Online (Sandbox Code Playgroud)