例如,如果我有一个像这样的.dat文件
410000 1905 7 50
410001 2015 3 25
410023 1857 12 -25
我试图找出如何将这些数字存储到链表中,我理解链表,但是在.dat可能看起来像
的情况下410000 1905 7 50
410001 2015 3 25
410023 1857 12
最后一行只有3个值,我不太清楚如何获取这些值。我最初拥有的是什么。
while(filename >> location >> year >> month >> temp)
但是我想知道在该行上是否只有3个值,我是否会收到错误或者是否会从下一行取下临时值,因为我认为这不起作用,我想知道getline是如何工作的,所以我想也许我可以试试
while ( getline(filename,location,year,month,temp))
我想知道如果那个while循环用3个值而不是4个点击到该行会发生什么。 因此,如果有人能够解释我如何解决问题,那么任何帮助都会受到赞赏。
如果一行上只有3个值,我必须告诉用户有错误,但继续检查所有其他值,我不能只返回0;并退出该程序。
答案 0 :(得分:0)
getline()
从istream
读取一整行。你的循环看起来像:
string line;
while (getline(file,line)) {
// parse the string in line to extract the compnents
...
}
分解line
的一种方法是使用stringstream
:
stringstream is(line);
is >> location >> year >> month >> temp;
如果在行的格式中发生错误,则is
的状态将相应地更改。