Basically as the title suggests. Basically, I have a text file (lyrics or subtitles, say) set out as so:
00:04:00 00:09:00 XXXXXXXXXXXXX
Where the ints are times (start and end times) and XXXXXXXX being the string.
How then would I be able to store this data into a vector?
I have a similar example where the text file uses years (ie 2000 2010 XXX) and I was able to store this in a vector and print to console without issues.
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
#include "timer.h"
usingnamespace std;
class Lyrics{
int _stime;
int _etime;
int _line;
string _lyric;
public:
//Construct lyrics with initial values for each field
Lyrics(int st, int et, int l, string &lc) : _stime(st), _etime(et), _line(l), _lyric(lc){}
//Selector functins for fields
int stime() {return _stime;}
int etime() {return _etime;}
int line() {return _line;}
const string & lyric() const {return _lyric;}
//Is the time right for the words?
bool rightTime(int y) const{
return _stime <= y && y <= _etime;
}
};
//Read lyrics from text file and add to vector.
void read_lyrics(vector<Lyrics> & v){
ifstream in("pok.txt");
int st, et, l;
string lc;
while(in >> st >> et >> l >> lc)
v.push_back(Lyrics(st, et, l, lc));
}
//Print lyrics
int main(){
//Timer timer;
vector<Lyrics> lyrics;
read_lyrics(lyrics);
for(int i=0; i <= lyrics.size(); i++){
Lyrics w = lyrics[i];
//if (w.rightTime())
cout << w.stime() << w.etime() << w.line() << w.lyric() << "\n";
//cout << "Test.\n";
}
return 0;
}
I also need a way that I can use time as an input so the words can show up based on the times from the text file (ie at 4 seconds line 1 is printed, at 7 seconds line 2....and so on).
The reason you can't do it right now is because the >> operator will read up to a space (or a newline) and then stop, however in this case you want to read up to the next ':'. I would suggest using std::getline() first, then parsing the string yourself with .find() and .substr().
I forgot to mention though that I get an error of "vector out of bounds". If I just gave main() a simple thing (a cout, for example) it compiles fine. So something in the for loop is giving me my error.
Also, even if the file formats has no use ':' and just ints, I still get the error.