我的循环似乎有一个大问题。 程序的作用:程序读取一个文本文件,并以字符串格式给出文本文件的每一行。我想要的字符串中的数据由制表符分隔,因此我正在解析,找到制表符并将所有字符相加,直到我点击第一个制表符。然后我将字符添加到选项卡并将其添加到数组中,以便稍后评估数据。
My Inner while Loop is parsing my first line correctly but is not iterating to the next line in the text file.
//Creates a temporary array
string current_line; //declare var line
string temp_string = "";
//When getline hits 'n' we are adding that to the string current_line
cout<<"Entering the Loop \n"<<endl;
// Gets the first line in the Text file and continutes to get each new line
// until we hit the end of file
while(getline(infile,current_line,'\n').good()){
//cout << "CURRENT SIZE " << current_line.size() << endl;
/* iterates through each character as long as the size of the current
line is greater than the counter i
*/
while(i < current_line.size()){
// If the Current character in the line is NOT a tab we add the
// character to string temp_storage
if(current_line[i] != '\t'){
temp_storage +=current_line[i];
}
/*If the Current Character in the line is a tab then we store
string temp_storage into another variable. Temp is cleared so we
can get the next word in the string
*/
else if (current_line[i] =='\t'){
Storage = temp_storage;
cout << "Current val: "<< Storage <<endl;
// Clears the temporary storage
temp_storage = "";
cout << "Clearing the temp_storage..."<< temp_storage<<endl;
}
//out << i << endl;
i++; // iterates the loop
}
cout<<"loop finished"<<endl;
}
//这里是运行程序的输出
感谢阅读!
最佳答案
如果您不喜欢像流迭代器这样的“现代”东西,我会按预期使用“旧”结构。试试 for
环形。 while 语句可以做任何事情,但如果你只是用计数器迭代,一个 for
loop 更适合。
如果你这样做了,你几乎不会出错:
for ( int i=0; i<current_line.size(); i++ )
在您的代码中,i
甚至可以在输入此代码段时有任何值。
关于C++ While循环不迭代到文本文件中的下一行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46500047/