C++ 使用getline在运行时获取与文件输入相关的错误
这不是作业,这是自学。我收到一个意外错误,认为是文件结束后getline请求的结果。我想我是在检查while(getline(inf,mystring))的输入是否成功,但它不起作用。如果不是这样,我如何有效地检查文件结尾 这是我的密码C++ 使用getline在运行时获取与文件输入相关的错误,c++,file-io,getline,C++,File Io,Getline,这不是作业,这是自学。我收到一个意外错误,认为是文件结束后getline请求的结果。我想我是在检查while(getline(inf,mystring))的输入是否成功,但它不起作用。如果不是这样,我如何有效地检查文件结尾 这是我的密码 #include <iostream> #include <string> #include <fstream> using namespace std; int main(int argc, char** argv) {
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main(int argc, char** argv)
{
string mystring, substring1 = "", substring2 = "";
int sos;
ifstream inf (argv[1]); //open file for reading
if (!inf)
{
// Print an error and exit
cerr << "Uh oh, " << argv[1] << " could not be opened for reading!" << endl;
exit(1);
}
while(getline(inf,mystring))
{
sos = mystring.find_first_not_of(" ");
if (sos != 0)
{
mystring = mystring.substr(sos, string::npos);
}
sos = mystring.find_first_of(" ");
if (sos != 0)
{
substring1 = mystring.substr(0,sos);
substring2 = mystring.substr(sos + 1, string::npos);
}
sos = substring2.find_first_of(" ");
if (sos != 0)
{
substring2 = substring2.substr(0, sos);
}
cout << substring2 << " " << substring1;
}
return 0;
}
这是输入文件input.in
Hello World
在提取子字符串之前,需要检查子字符串的范围是否为物理范围(即,调用$substr$的第一个索引位于最后一个索引之前)。我怀疑您的文件包含一个空行,在本例中,调用$find_first_not_of$将返回$npos$,表明在字符串结尾之前未找到任何内容 我建议为$find\u first\u not\u of$返回的$npos$添加一张支票:
// Strip leading spaces
sos = mystring.find_first_not_of(" ");
/* sos == 0 would mean substr(0, npos) -> useless.
sos == npos would mean substr(npos, npos) -> un-physical. */
if (sos != 0 && sos != string::npos)
{
mystring = mystring.substr(sos, string::npos);
}
// Split string by first space
sos = mystring.find_first_of(" ");
substring1 = mystring.substr(0, sos); /* sos != 0, since strip leading spaces */
/* Check that sos+1 is inside the string. */
if (sos+1 < mystring.length())
{
substring2 = mystring.substr(sos+1, string::npos);
}
else
{
substring2 = "";
}
sos = substring2.find_first_of(" ");
/* sos == 0 would cause substr(0, 0) -> un-physical,
sos == npos would cause substr(0, npos) -> useless. */
if (sos != 0 && sos != string::npos)
{
substring2 = substring2.substr(0, sos);
}
count << substring2 << ' ' << substring1 << std::endl;
//带前导空格
sos=mystring.find_first_not_of(“”);
/*sos==0意味着substr(0,NPO)->无用。
sos==npos意味着substr(npos,npos)->非物理的*/
if(sos!=0&&sos!=string::npos)
{
mystring=mystring.substr(sos,string::npos);
}
//按第一个空格分割字符串
sos=mystring.find_first_of(“”);
substring1=mystring.substr(0,sos);/*求救0,因为带前导空格*/
/*检查sos+1是否在字符串内*/
如果(sos+1非物理,
sos==NPO将导致substr(0,NPO)->无效*/
if(sos!=0&&sos!=string::npos)
{
子字符串2=子字符串2.子字符串(0,sos);
}
count注意,find_first_not_of在未找到任何内容时返回string::npos,而不是在测试时返回零。
如果有一行没有空格(或空行),那么对find_first_not_of(“”)的测试将返回字符串::npos导致
mystring = mystring.substr(string::npos, string::npos);
这将导致异常。请使用调试器并将崩溃精确定位到一行代码。这会让我们大家的生活更轻松。文件中有空行吗?你到底想做什么?应该读入一行或多行,去掉前导空格和尾随空格,然后交换每行上的两个单词。@WilliamMichaelVondran然后我会为你提供更好的代码。
mystring = mystring.substr(string::npos, string::npos);