[英]Garbage value on reading from a file using getline
我正在編寫一個小程序,以特定格式從srt文件中獲取行。 但是,我在使用getline所做的第一個(也是唯一一個)閱讀中獲得了一個垃圾值。 有人可以指出為什么我會出現這種異常行為嗎?
//Small program to get the text from the srt files.
#include<iostream>
#include<string>
#include<fstream>
#include<algorithm>
#include<vector>
#include<sstream>
#include<conio.h>
using namespace std;
void srtToTranscript(ifstream* iFile, ofstream *oFile);
int main(){
std::string file_name;
std::string transcript;
cout << "Enter srt file name (without extension)";
cin >> file_name;
ifstream iFile;
ofstream oFile;
iFile.clear();
iFile.open("data\\" + file_name+".srt");
oFile.open("data\\" + file_name + "_result.txt");
srtToTranscript(&iFile,&oFile);
cout << "Conversion done. Check in the same folder";
cout << "Press a key to exit... ";
while (!_kbhit());
char dummy = _getch();
return 0;
}
void srtToTranscript(ifstream* iFile, ofstream* oFile)
{
int i = 1;
std:string line;
for (; getline(*iFile, line);)
{
cout << line << endl;
cout << to_string(i) << endl;
if (line.compare(to_string(i)) == 0){
getline(*iFile, line);
i++;
continue;
}
*oFile << line + ",\n";
}
oFile->close();
}
附件是我正在讀取的文件的示例:
1
00:00:00,000-> 00:00:12,000
譯者:哈周紅
2
00:00:12,038-> 00:00:15,012
在過去的二十年中,印度已成為
問題是您的ifile
未成功打開。
我在您的包含列表中看到#include <conio.h>
,因此我假設您正在使用Visual Studio? 如果在設置Visual Studio時使用了默認的目錄結構,則說您有一個名為“ Foo”的項目。.srt文件需要轉到此處:
... /文檔/ Visual Studio 20 ## /項目/Foo/Foo/data/Foo.srt
當我正確地將文件放在此處並在提示符下輸入時:
富
我得到的輸出是:“ ... / Documents / Visual Studio 20 ## / Projects / Foo / Foo / data / Foo_result.txt”:
00:00:00,000-> 00:00:12,000,
,
譯者:哈周Hu
,
00:00:12,038-> 00:00:15,012,
,
在過去的二十年中,印度已成為
我要確保的一件事是定義了srtToTranscript
中的line
聲明:
string line
不:
std:string line
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.