gpt4 book ai didi

c++ - 解析文本文件时抛出 std::out_of_range

转载 作者:太空宇宙 更新时间:2023-11-04 14:57:02 30 4
gpt4 key购买 nike

我有以下代码来读取文本文件。

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
...

我做错了什么?

最佳答案

问题1

while .goodwhile !.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;
}
}

问题2

您没有使用 std::string::find正确。

line.find("PackageId:")返回“搜索内容在字符串中第一次出现的位置”,或者返回成员值 npos如果未找到匹配项。

这与不对 std::string::substr 的第一个参数执行边界检查相结合导致您的字符串出现问题。

相反,写:

if (line.find("PackageId:") != std::string::npos)

问题3

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/

30 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com