C++:从文本文件中读取并分离成变量
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3946558/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
C++: Read from text file and separate into variable
提问by diehell
I have this in a text file:
我在一个文本文件中有这个:
John 20 30 40
mike 30 20 10
How do i read from the text file and separate them into variable name, var1, var2, var3. This is my attempt, seems it doesnt work. Help please.
我如何从文本文件中读取并将它们分成变量名、var1、var2、var3。这是我的尝试,似乎不起作用。请帮忙。
#include <iostream>
#include <fstream>
#include <string>
#include <sstream>
using namespace std;
int main () {
string name,result;
int number1;
ifstream myfile ("marks.txt");
if (myfile.is_open())
{
while ( !myfile.eof() )
{
getline (myfile,name,'\t');
getline (myfile,var1,'\t');
getline (myfile,var2,'\t');
getline (myfile,var3,'\t');
cout << name << var1 << var2 << var3;
}
myfile.close();
}
else cout << "Unable to open file";
return 0;
}
EDIT 1:
编辑 1:
Nocturne Suggestion:
夜曲推荐:
#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;
int main()
{
ifstream inputFile("marks.txt");
string line;
while (getline(inputFile, line))
{
istringstream ss(line);
string name;
int var1, var2, var3;
ss >> name >> var1 >> var2 >> var3;
cout << name << var1 << var2 << var3 << endl << endl;
}
}
output:
输出:
John203040
mike302010
302010
Why another 302010???
为什么是另一个 302010???
回答by PlagueHammer
Something like this should work (I don't have a compiler handy, so you may need to tweak this a little):
这样的事情应该可以工作(我手头没有编译器,所以你可能需要稍微调整一下):
#include <iostream>
#include <sstream>
using namespace std;
int main()
{
ifstream inputFile("marks.txt");
string line;
while (getline(inputFile, line))
{
istringstream ss(line);
string name;
int var1, var2, var3;
ss >> name >> var1 >> var2 >> var3;
}
}
Edit: Just saw this again, I don't know why I chose the get line approach earlier. Doesn't the following (simpler solution) work?
编辑:刚刚又看到了这个,我不知道为什么我之前选择了 get line 方法。以下(更简单的解决方案)不起作用吗?
#include <fstream>
using namespace std;
int main()
{
ifstream fin(“marks.txt”);
string name;
int var1;
int var2;
int var3;
while (fin >> name >> var1 >> var2 >> var3)
{
/* do something with name, var1 etc. */
cout << name << var1 << var2 << var3 << “\n”;
}
}
回答by JoshD
It looks like you need to declare var1, var2, and var3.
看起来你需要声明var1、var2和var3。
Also instead of this:
也不是这个:
getline (myfile,name,'\t');
getline (myfile,var1,'\t');
getline (myfile,var2,'\t');
getline (myfile,var3,'\t');
Try this:
尝试这个:
myfile >> name;
myfile >> var1;
myfile >> var2;
myfile >> var3;
Not because what you had is wrong, but the second is cleaner and will handle all white space.
不是因为你有什么是错的,但第二个更干净,将处理所有的空白。
回答by Lisa
I think that when the numbers were printed out in a group with no spaces, the reason was just that you need to put spaces in between the variable, like this:
我认为当数字在没有空格的组中打印出来时,原因只是您需要在变量之间放置空格,如下所示:
cout << name << " " << var1 << " " << var2 << " " << var3 << "\n";