如何跳到文件中的新行并重复循环
How to skip to a new line in a file and repeat loop?
我正在尝试从如下所示的文件中读取数据:
1 1 2007 12 31 2006
12 31 2007 1 1 2008
1 3 2006 12 15 2006
2 29 1900 3 1 1900
9 31 2007 10 28 2009
我正在使用一个函数以 3 int
s 为一组读取它,因为这些值应该是日期,每行有 2 个日期。如果货币对的第一个日期返回错误,我需要跳到下一行并从那里开始重复循环,如果没有错误,只需评估下一组。错误的文件和bool
变量将作为引用参数传递到 Get_Date()
函数中,以便它们在函数外部相应地更改。我尝试使用 ignore()
和如下break
语句:
while (infile) {
Get_Date(infile, inmonth1, inday1, inyear1, is_error);
if (is_error == true){
infile.ignore(100, 'n');
break;
}
if (is_error == false) {
Get_Date(infile, inmonth2, inday2, inyear2, is_error);
if (is_error == false) {
Get_Date(infile, inmonth2, inday2, inyear2, is_error);
other stuff;
}
}
}
这只是使整个程序在收到 1 个错误后终止,在第 4 行,因为 2 月在闰年只有 29 天。我以为中断会将控制权返回到最近的循环,但这似乎不是正在发生的事情。
一旦发现错误,您可以使用continue
跳过循环其余部分的执行:
while (infile)
{
Get_Date(infile, inmonth1, inday1, inyear1, is_error);
if (is_error)
{
//Skip the rest of the line (we do not know how long it is)
infile.ignore(std::numeric_limits<std::streamsize>::max(), 'n');
continue; //Skip the rest of the cycle
}
Get_Date(infile, inmonth2, inday2, inyear2, is_error);
if (is_error) { continue; } //Skip the other stuff if we have error
otherStuff();
}
您可以在以下位置的 Jump 语句下找到更多信息: http://www.cplusplus.com/doc/tutorial/control/
相关文章:
- 使用新行和不使用新行读取文件
- 遇到新行时,有没有办法停止istream_iterator
- PangoLayout 中忽略的新行
- 使用 clang 格式时,当行很大时,如何将右括号分成新行?
- 如何检测将文本文件读入 int 数组的新行
- 当 getline 直到新行才读取时会发生什么?
- 添加新行时工作代码引发异常.调试技巧?
- QTree查看新行,没有布局就不可见已更改
- 新行上的 clang 格式右括号
- C++流:保存到新行而不是覆盖旧行
- (C++) 使用诅咒打印从文件中读入的新行
- 一个 2D 数组,并按行存储值.第一个循环用于行索引,第二个循环用于列索引
- 按升序输出整数时,将最后一个整数放在新行上
- 用C++计算输入文本的新行数
- 为什么我的代码在终端中没有输出任何内容.开始新行没有错误
- WriteConsoleOutputCharacterW - 我的新行在哪里?
- 如何运行 while 循环直到到达新行?
- C++:进入循环后增加新行
- 仅在向量末尾打印新行 - "if"语句在"for"循环之外
- 如何跳到文件中的新行并重复循环