timmu
10th September 2012, 12:14
What is the best way to stop QTextStream input at end of file.
This reads past end of file because apaprently the end of row character is considered an entry. What clause to use with QTextStream to detect for that? Normally I would use !file.eof() but this does not work here. My input file consists of several rows of numbers (all rows contain the same number of values).
My code
QFile file(Name); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
QTextStream my_stream(&file);
while(!my_stream.atEnd())
{
for (k=0; k<100; k++)
{
double readInput;
my_stream >> readInput;
}
}
This would work but it's not elegant:
(and it actually has a problem since one wrong value is read)
bool OK=1;
while(!my_stream.atEnd() && OK)
{
for (k=0; k<100; k++)
{
if(OK)
{
double readInput;
my_stream >> readInput;
}
if(my_stream.atEnd()) OK=0;
}
}
I need to read my file one entry at a time but readLine() wouldn't work since I don't want to convert QString to double every time.
This reads past end of file because apaprently the end of row character is considered an entry. What clause to use with QTextStream to detect for that? Normally I would use !file.eof() but this does not work here. My input file consists of several rows of numbers (all rows contain the same number of values).
My code
QFile file(Name); if(!file.open(QIODevice::ReadOnly | QIODevice::Text)) {exit(1);}
QTextStream my_stream(&file);
while(!my_stream.atEnd())
{
for (k=0; k<100; k++)
{
double readInput;
my_stream >> readInput;
}
}
This would work but it's not elegant:
(and it actually has a problem since one wrong value is read)
bool OK=1;
while(!my_stream.atEnd() && OK)
{
for (k=0; k<100; k++)
{
if(OK)
{
double readInput;
my_stream >> readInput;
}
if(my_stream.atEnd()) OK=0;
}
}
I need to read my file one entry at a time but readLine() wouldn't work since I don't want to convert QString to double every time.