我想知道是否有任何方法只使用C++而不使用seekg()从一行文本中获取整数。从.txt文件的一行输入只有整数
比方说我的文件data.txt
只有这条线:Position {324,71,32}
在里面,我只想得到整数值。
我试过下面的代码,但它没有工作,我已经在网上搜索解决方案,并没有找到任何 - 这就是为什么我问。
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
string x, num1, num2, num3;
fstream fs;
fs.open("data.txt", ios::in);
if (fs.fail())
cerr << "Failed to open file";
fs >> x;
num1 = x;
fs >> x;
num2 = x;
fs >> x;
num3 = x;
cout << num1 << " " << num2 << " " <<num3 << endl;
return 0;
}
http://stackoverflow.com/a/2084366/179910 – 2014-09-10 18:06:28