的特定部位我有以下內容輸入文件:讀取和解析文件
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因爲我只想要文件中的時間戳,而不是其他的時間戳。這是該文件中字符串的「子字符串」。 –