我正在编写一个C ++项目,但我遇到了一些问题。我有一个文本文件:
5.123456789
9.987654321
6
我想有这样的作业:
double firstLine = 5.123456789;
double secondLine = 9.987654321;
int thirdLine = 6;
如何直接从文本文件中分配值?
这是我到目前为止所做的:
string line = "";
ifstream infile;
infile.open("xinput.txt");
while(getline(infile,line))
{
//I suppose this is where I would have to insert my missing code,
//but I am unsure how to proceed.
}
infile.close();
答案 0 :(得分:1)
如果我正确理解了您的需求,您想知道如何将.txt中的值分配给变量。
如果是,你需要这样的东西:
ifstream infile;
infile.open("xinput.txt");
double firsLine;
double secondLine;
int thirdLine;
while (!infile.eof())
{
getline(infile, firstLine, '\n');
getline(infile, secondLine, '\n');
getline(infile, thirdLine, '\n');
}
infile.close();
对于你的未来问题,你需要这些:
https://stackoverflow.com/help/how-to-ask
http://codeblog.jonskeet.uk/2010/08/29/writing-the-perfect-question/