[英]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.