I am trying to get some data from a text file that are in specific lines (1st, 7th, 13th, etc - data needed is placed at the next 6th-line)
My code so far is this:
txtfile = "titles.txt";
ifstream txt(txtfile);
const int buffer_size = 80;
char title_buffer[buffer_size];
const int titleLineDiff = 6;
if (txt.is_open())
{
while(!txt.eof())
{
static int counter = 1;
txt.getline(title_buffer, buffer_size);
cout << "Title: \"" << counter << "." << title_buffer << "\"" << endl;
counter++;
//seek to the next title...difference is 6 lines
for(int i = 0; i < titleLineDiff; i++)
txt.getline(title_buffer, 40);
}
}
Now, it works fine with this file I've created:
testONE
two
three
four
five
six
testTWO
bla
And it prints "testONE" and "testTWO" but when I am trying to open the file that contains the data, I get an infinite loop and the output is
Title: "counter_increasing_number."
The text document, was copied from the internet and this might be the cause of the problem in reading it.
What can I do about this?
I've changed the code to this:
while(getline(txt,title_buffer))
{
static int counter = 1;
//getline(title_buffer, buffer_size);
cout << "Title: \"" << counter << "." << title_buffer << "\"" << endl;
counter++;
//seek to the next title...difference is 6 lines
for(int i = 0; i < titleLineDiff; i++)
{
getline(txt, title_buffer);
}
}
and it worked.
Could somebody explain me the reason why the first didn't work?