我正在读取文本文件,我需要能够访问类似于Java使用nextInt()的每个整数。现在,如果我有一个名为"输入"和变量" x"类型INT,input >> x
将忽略行中的所有空格,并给我一个大整数,而不是只给我行中的第一个整数。
例如,如果我的文本文件如下所示:
5 67 8
12 3 4
当我说input >> x
时,x现在的值为#34; 5678"而不只是' 5'然后' 67'我下次打电话的时候。
我该如何解析?
答案 0 :(得分:0)
mgl @ mgl:〜/ Documents $ cat int_file.log
45 20 12 45 21 25
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
using namespace std;
int main(){
vector<int> words;
ifstream in("int_file.log");
int word;
while( in >> word)
words.push_back(word);
for(int i = 0; i < words.size();i++)
cout << words[i] << endl;
return 0;
}
我希望它可以帮到你。
答案 1 :(得分:0)
以下代码按预期工作:
int main() {
std::ifstream input ("test.txt", std::ifstream::in);
int x, y, z;
input >> x;
input >> y;
input >> z;
std::cout << "x = " << x << " y = " << y << " z = " << z << std::endl;
}