使用比较运算符和字符串比较函数读取文本文件的一部分时遇到问题
Trouble with reading a subsection of a text file using comparison operators and string compare function
因此,我正在尝试读取以下文本文件中 400 和 ***** 之间的部分:
400
http://csweb.cs.wfu.edu
http://college.wfu.edu/cs/wp-content/themes/wfcollegeonepro
http://college.wfu.edu/cs/sample-page/feed
http://college.wfu.edu/cs/wp-includes/wlwmanifest.xml
http://college.wfu.edu/cs
http://www
http://www.wfu.edu
http://college.wfu.edu
http://college.wfu.edu/cs/news
*****
16331
http://college.wfu.edu/cs/wp-content/themes/wfcollegeonepro http://csweb.cs.wfu.edu
http://college.wfu.edu/cs/sample-page/feed http://csweb.cs.wfu.edu
http://college.wfu.edu/cs/wp-includes/wlwmanifest.xml http://csweb.cs.wfu.edu
http://college.wfu.edu/cs http://csweb.cs.wfu.edu
http://www http://csweb.cs.wfu.edu
http://www.wfu.edu http://csweb.cs.wfu.edu
我编写了以下代码(我很确定)完成了任务:
file2.open("./cswebDataCopy2.txt");
cout << "Opening cswebData.txt file..." << endl;
if(!file2)
{
cout << "System failed to open cswebData.txt..." << endl;
}
else
{
cout << "cswebData.txt successfully opened!" << endl;
}
cout << "READING FROM: cswebData.txt" << endl;
while(!file2.eof())
{
getline(file2,line4);
//cout << line4 << endl;
if(line4=="400")
{
cout << "Number of vertices in cswebData.txt: " << line4 << endl;
continue;
}
else if(line3=="*****")
{
cout << "Found the ****** " << endl;
break;
}
else
{
cout << "Couldn't find the 400 or ***** " << endl;
}
}
cout << "Code after while loop" << endl;
file2.close();
但是,当我 运行 代码时,它只打印 else 语句中的代码,次数与文件中的行数一样多,然后是 while 循环之后的代码,即使第 400 行和 ***** 清楚地在文件中。所以,我想我会打印出正在阅读的行,以防万一被跳过。事实证明,程序正在正确读取所有行。然后,我认为这可能与我在 if 和 else-if 语句中进行比较的方式有关。所以,我继续使用字符串比较函数重新编写代码,如下所示:
while(!file2.eof())
{
getline(file2,line4);
//cout << line4 << endl;
if(line4.compare("400")==0)
{
cout << "Number of vertices in cswebData.txt: " << line4 << endl;
continue;
}
else if(line4.compare("*****")==0)
{
cout << "Found the ***** " << endl;
break;
}
else
{
cout << "Couldn't find 400 or the ***** " << endl;
}
}
cout << "Code after the while loop" << endl;
但是,在 运行我的代码的第二个版本之后,我仍然得到与我的代码的第一个版本相同(错误)的结果。在这一点上,我对为什么我的代码不起作用感到有点困惑。如果有人有敏锐的洞察力,那就太好了。谢谢!
所以,事实证明问题与我的代码的任何一个版本都无关。显然,我的 Sublime 文本文件中有一些隐藏的字符会影响文件的读取。当我将原始文本文件的内容复制并粘贴到 b运行d 新文件中,然后将我的代码的两个版本 运行 复制到新文本文件中时,一切都按预期进行。谢谢大家的意见!
因此,我正在尝试读取以下文本文件中 400 和 ***** 之间的部分:
400
http://csweb.cs.wfu.edu
http://college.wfu.edu/cs/wp-content/themes/wfcollegeonepro
http://college.wfu.edu/cs/sample-page/feed
http://college.wfu.edu/cs/wp-includes/wlwmanifest.xml
http://college.wfu.edu/cs
http://www
http://www.wfu.edu
http://college.wfu.edu
http://college.wfu.edu/cs/news
*****
16331
http://college.wfu.edu/cs/wp-content/themes/wfcollegeonepro http://csweb.cs.wfu.edu
http://college.wfu.edu/cs/sample-page/feed http://csweb.cs.wfu.edu
http://college.wfu.edu/cs/wp-includes/wlwmanifest.xml http://csweb.cs.wfu.edu
http://college.wfu.edu/cs http://csweb.cs.wfu.edu
http://www http://csweb.cs.wfu.edu
http://www.wfu.edu http://csweb.cs.wfu.edu
我编写了以下代码(我很确定)完成了任务:
file2.open("./cswebDataCopy2.txt");
cout << "Opening cswebData.txt file..." << endl;
if(!file2)
{
cout << "System failed to open cswebData.txt..." << endl;
}
else
{
cout << "cswebData.txt successfully opened!" << endl;
}
cout << "READING FROM: cswebData.txt" << endl;
while(!file2.eof())
{
getline(file2,line4);
//cout << line4 << endl;
if(line4=="400")
{
cout << "Number of vertices in cswebData.txt: " << line4 << endl;
continue;
}
else if(line3=="*****")
{
cout << "Found the ****** " << endl;
break;
}
else
{
cout << "Couldn't find the 400 or ***** " << endl;
}
}
cout << "Code after while loop" << endl;
file2.close();
但是,当我 运行 代码时,它只打印 else 语句中的代码,次数与文件中的行数一样多,然后是 while 循环之后的代码,即使第 400 行和 ***** 清楚地在文件中。所以,我想我会打印出正在阅读的行,以防万一被跳过。事实证明,程序正在正确读取所有行。然后,我认为这可能与我在 if 和 else-if 语句中进行比较的方式有关。所以,我继续使用字符串比较函数重新编写代码,如下所示:
while(!file2.eof())
{
getline(file2,line4);
//cout << line4 << endl;
if(line4.compare("400")==0)
{
cout << "Number of vertices in cswebData.txt: " << line4 << endl;
continue;
}
else if(line4.compare("*****")==0)
{
cout << "Found the ***** " << endl;
break;
}
else
{
cout << "Couldn't find 400 or the ***** " << endl;
}
}
cout << "Code after the while loop" << endl;
但是,在 运行我的代码的第二个版本之后,我仍然得到与我的代码的第一个版本相同(错误)的结果。在这一点上,我对为什么我的代码不起作用感到有点困惑。如果有人有敏锐的洞察力,那就太好了。谢谢!
所以,事实证明问题与我的代码的任何一个版本都无关。显然,我的 Sublime 文本文件中有一些隐藏的字符会影响文件的读取。当我将原始文本文件的内容复制并粘贴到 b运行d 新文件中,然后将我的代码的两个版本 运行 复制到新文本文件中时,一切都按预期进行。谢谢大家的意见!