[英]How can i skip blank lines when reading from a file in C++?
How can I skip blank lines when reading from a file in c++? 在c ++中读取文件时如何跳过空白行? the set up is: I have a text file with a set of 50 questions each one followed by four possible answers.
设置是:我有一个文本文件,每个文件包含50个问题,然后是四个可能的答案。 I am using a 2d array to store all of this: questions[50][5]
我正在使用2D数组存储所有这些内容:问题[50] [5]
so the text file is structured like this: 因此文本文件的结构如下:
1 line length questions 1行长度的问题
answer a 回答一个
answer b 回答b
answer c 回答c
answer d 回答d
1 line length questions 1行长度的问题
answer a 回答一个
answer b 回答b
answer c 回答c
answer d 回答d
and so on.. 等等..
there is no gap between the question or any of the answers, but between each group of question and 4 answers, there is an unknown amount of blank lines. 在一个问题或任何一个答案之间没有间隙,但是在每组问题和四个答案之间,没有空白行。
I'm using a loop to input the question and for answers into my array for each set of questions, but how can I skip the unknown amount of blank lines for each set of questions? 我正在使用循环将问题和答案输入每组问题的数组中,但是如何跳过每组问题的未知行数呢? here is my code
这是我的代码
//reading in the questions
for(int i = 0; i < 50; i++)
{
while(getline(qFileIn,test)) //seeing if its empty
{
if(test.empty())
continue;
if(!(test.empty()))
break;
}
for (int j = 0; j < 5; j++)
{
getline(qFileIn,questions[i][j]);
}
}
I'm really struggling with trying to skip the blank lines somewhere within those two for loops, and i'd much appreciate some help. 我真的很想尝试跳过这两个for循环中的空白行,我非常感谢您的帮助。 Thanks!!!
谢谢!!!
Try this maybe qFileIn.ignore(256, '\\n'); 试试这个也许qFileIn.ignore(256,'\\ n'); it skips empty lines.
它跳过空行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.