- android - 多次调用 OnPrimaryClipChangedListener
- android - 无法更新 RecyclerView 中的 TextView 字段
- android.database.CursorIndexOutOfBoundsException : Index 0 requested, 光标大小为 0
- android - 使用 AppCompat 时,我们是否需要明确指定其 UI 组件(Spinner、EditText)颜色
我有以下代码来读取文本文件。
const string FILENAME = PACKAGES_DIR + pname;
//the arguments to ifstream is a cstring and hence the conversion must be made
ifstream freader;
freader.open(FILENAME.c_str(),ios::in);
if(freader.is_open())
{
while(freader.good())
{
string line;
getline(freader,line);
cout<<line<<endl;
if(line.find("PackageId:"))
{
cout<<line.substr(11)<<endl;
}
else if(line.find("Name:"))
{
cout<<line.substr(5)<<endl;
}
else if(line.find("Version:"))
{
cout<<line.find(8)<<endl;
}
else
{
cout<<line<<endl;
}
}
}
有问题的文本文件的内容是
PackageId:994
Name:basket
Version:1.80-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
我得到的输出是
PackageId:994
geId:994
Name:basket
Version:1.80-1
0-1
Deps:kdebase-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
e-runtime,libc0.1,libc0.1-udeb,libc6,libc6-udeb,libc6.1,libc6.1-udeb,libgcc1,libgpg-error0,libgpgme11,libkdecore5,libkdeui5,libkfile4,libkio5,libkparts4,libkutils4,libphonon4,libqimageblitz4,libqt4-dbus,libqt4-network,libqt4-qt3support,libqt4-svg,libqt4-xml,libqtcore4,libqtgui4,libstdc++6,libunwind7,libx11-6,phonon
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr
我想要的输出是:
PackageId:994
994
Name:basket
basket
Version:1.80-1
1.80-1
...
我做错了什么?
最佳答案
while .good
或 while !.eof
几乎总是错的。扔掉任何告诉你这样做的书,然后 do this instead .
在这种情况下,更改后的代码看起来有点像这样:
const string FILENAME = PACKAGES_DIR + pname;
//the arguments to ifstream is a cstring and hence the conversion must be made
ifstream freader(FILENAME.c_str(), ios::in);
if (freader) {
string line;
while (getline(freader,line)) { // <-----
cout << line << endl;
if (line.find("PackageId:"))
cout << line.substr(11) << endl;
else if (line.find("Name:"))
cout << line.substr(5) << endl;
else if (line.find("Version:"))
cout << line.find(8) << endl;
else
cout << line << endl;
}
}
您没有使用 std::string::find
正确。
line.find("PackageId:")
返回“搜索内容在字符串中第一次出现的位置”,或者返回成员值 npos
如果未找到匹配项。
这与不对 std::string::substr
的第一个参数执行边界检查相结合导致您的字符串出现问题。
相反,写:
if (line.find("PackageId:") != std::string::npos)
cout<<line.find(8)<<endl;
应该说substr
, 不是 find
.
你的代码修复了上面的一些问题:
const string FILENAME = PACKAGES_DIR + pname;
//the arguments to ifstream is a cstring and hence the conversion must be made
ifstream freader(FILENAME.c_str(), ios::in);
if (freader) {
string line;
while (getline(freader,line)) { // <-----
cout << line << endl;
if (line.find("PackageId:") != std::string::npos && line.size() > 11)
cout << line.substr(11) << endl;
else if (line.find("Name:") != std::string::npos && line.size() > 5)
cout << line.substr(5) << endl;
else if (line.find("Version:") != std::string::npos && line.size() > 8)
cout << line.substr(8) << endl;
else
cout << line << endl;
}
}
关于c++ - 解析文本文件时抛出 std::out_of_range,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7812370/
这段代码是关于将字符串转换为替代密码(将特定字母替换为另一个特定字母,例如:用'E'代替'a')当我输入字符串时,它可以正常工作。但是,当我输入一个句子时,它说“在抛出一瞬间'std::out_of_
我编写了一个对字符串进行百分号编码的函数,如下所示: string percent_encode(string str) { string reserved = // gen-delims
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有一个非常简单的程序,可以将十六进制字符串转换为它的 int 值。代码看起来不错,但会引发运行时错误: terminate called after throwing an instance of
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我正在尝试制作一个基本的计算器,用户可以在其中输入 2 个数字和一个运算符,然后计算就可以进行了。我不明白为什么我会收到这个错误,我也不完全确定这个错误是什么意思,因为我是 c++ 的新手,只有一些
给定一个包含 ASCII 码和相应数值的全局 vector list 以及一个字符串,例如 000.00-000.0.0.0,此函数接受一个输入 token strings 2-char or 3-c
嗨,我正在点击 std::out_of_range: basic_string::substr在下面的代码中。一直在努力修复它,但无法完成。 std::string orig_str = "1-1,2
目前在 uni 做一个项目,起初我需要取消字符串的连字符,看起来很简单但是当我运行程序时它有一个错误 WeirdPuncProgram.exe: Microsoft C++ exception: st
下面是发生这个错误的函数: std::string DataTranslation::getMeshName(std::string meshLink) { File input(this->
我正在处理一个包含行链接列表的文件,每个节点如下所示: struct TextLine{ //The actual text string text; //The line n
我在 IDE 输出日志中收到此异常消息,即使我有一个代码可以捕获它,如果它发生的话。 这是代码本身: t_ptr obj_ptr; try { obj_ptr = _objects.at(fi
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我有以下代码来读取文本文件。 const string FILENAME = PACKAGES_DIR + pname; //the arguments to ifstream is a cstr
这个问题在这里已经有了答案: Why does std::getline() skip input after a formatted extraction? (5 个答案) 关闭 6 年前。 in
这个问题不太可能帮助任何 future 的访问者;它只与一个小的地理区域、一个特定的时间点或一个非常狭窄的情况有关,这些情况并不普遍适用于互联网的全局受众。为了帮助使这个问题更广泛地适用,visit
我正在 Linux Ubuntu 的 opencv 中编写以下代码。x_captured 和 y_captured 是“int”类型的 vector 。两个 vector 的大小都是 18。 for
我正在尝试编写一个简单的链表,并在用户需要超出范围的节点索引时尝试抛出 out_of_range 异常。但是,当我编译源文件时,出现错误“‘out_of_range’不是‘std’的成员”。 我的理解
考虑以下无法编译的代码: #include //#include // uncommenting this works int main() { std::map test; t
// The following code works fine, throwing a std::out_of_range exception: std::vector vd{ 1.
我是一名优秀的程序员,十分优秀!