的特定部位我有以下内容输入文件:读取和解析文件
Tstart: 13:51:45
Tend: 13:58:00
,我想有单独的字符串的时间戳结尾。到目前为止,我已经写了以下内容:
// open the info file
if (infile.is_open())
{
// read the info regarding the played video
string line;
while (getline(infile, line))
{
istringstream iss(line);
string token;
while (iss >> token)
{
string tStart = token.substr(0, 6);
string tEnd = token.substr(7,2);
cout << tStart << tEnd<< endl;
}
}
infile.close();
}
else
cout << "Video info file cannot be opened. Check the path." << endl;
,我得到下面的输出:
Tstart
13:51:5
terminate called after throwing an instance of 'std::out_of_range'
what(): basic_string::substr: __pos (which is 7) > this->size() (which is 5)
我理解错误说什么,但我无法找到用C这样做的另一种方式++ 。
任何人有想法?
你为什么使用'substr'? – LogicStuff
您没有按照您的意愿阅读文件。我怀疑你是复制了代码而没有完全理解它。如果您显示实际文件,则您执行'substr()'的第一个标记为“Tstart:”。调试器会向您显示正在读取的内容。 – stefaanv
@LogicStuff因为我只想要文件中的时间戳,而不是其他的时间戳。这是该文件中字符串的“子字符串”。 –