I am writing a small program to just get the lines from a srt file in a specific format. However I am getting a garbage value in the very first (and only that) read I do using getline. Can someone point out why am I getting this abnormal behaviour?
//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();
}
appended is a sample of the file I am reading from:
1
00:00:00,000 --> 00:00:12,000
Translator: Thu-Huong Ha
2
00:00:12,038 --> 00:00:15,012
Over the last two decades, India has become
The problem is your ifile
is not being opened successfully.
I'm seeing #include <conio.h>
in your include list so I assume that you are working on Visual Studio? If you have use the default directory structure when setting up Visual Studio, then say that you have a project named: "Foo" Your .srt file needs to go here:
.../Documents/Visual Studio 20##/Projects/Foo/Foo/data/Foo.srt
When I correctly placed the file there and at the prompt I entered:
Foo
I got this output in: ".../Documents/Visual Studio 20##/Projects/Foo/Foo/data/Foo_result.txt":
00:00:00,000 --> 00:00:12,000,
,
Translator: Thu-Huong Ha,
,
00:00:12,038 --> 00:00:15,012,
,
Over the last two decades, India has become,
One thing I'd make sure of is that your line
declaration in srtToTranscript
is defined:
string line
Not:
std:string line
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.