Reputation: 11
I am making a program that reads a txt file. I want to read some specific lines from a file and add them to ComboBox. (in my case would be line number: 1,6,11,16...)
I have only this that reads all the lines.
if(file.open (QIODevice::ReadOnly | QIODevice::Text))
{
while(!stream.atEnd())
{
line = stream.readLine ();
if(!line.isNull ())
{
ui->ServersNames->addItem (line);
}
}
}
stream.flush ();
file.close ();
Upvotes: 0
Views: 1348
Reputation: 6805
According to me, you cannot go to a specific line without knowing the line(s) size(s) since seek() can only move the cursor into a specific position value.
The only solution I can see is the one suggested by @Botje.
Based on your code, you could write:
if(file.open(QIODevice::ReadOnly | QIODevice::Text))
{
int nb_line(0);
while(!stream.atEnd())
{
line = stream.readLine();
if((nb_line % 5) == 1)
ui->ServersNames->addItem(line);
++nb_line;
}
file.close();
}
Of course, it assumes that you want to read one in five lines from the first line until the end of the file.
Upvotes: 2