PDA

View Full Version : Reading File using QFile and QTextStream



atm
12th July 2006, 23:00
Hi all,

I am trying to read in a tab-delimited text file. The code I wrote was first written using standard strings and infiles, and in the process of moving to Qt I am switching to the QFiles, QStrings, QTextStreams, and so on.

What I want to do is read in each line, parse out certain "keys" (or certain particular parts of the line, as defined by the tabs) and store these into QStrings.

Here is what I have:

QFile inFile(geneDesIn);

if ( inFile.open( QIODevice::ReadOnly | QIODevice::Text ) ) {
QString temp2, temp3, temp6, temp15, line;
QTextStream stream( &inFile );

for (int counter = 3; counter < 3765; counter++) {
line = stream.readLine(); // line of text excluding '\n'

//we now have a line of text with a bunch of tabs inside it
//we want to parse it, tab by tab, and get out the fields we want: 2 3 6 15

temp2 = line.section('\t', 1,1);
temp3 = line.section('\t', 2,2);
temp6 = line.section('\t', 5,5);
temp15 = line.section('\t', 14,14);

//debug output to see the contents of the line
cout << "LINE: " << line.toStdString() << endl;

//line is now loaded

//do stuff with the temp strings
}
inFile.close();
}

However, I am having some issues.
Instead of reading in each line individually, it reads in the *entire* text file as one single line. It must not be detecting end-of-line characters or something.
Any ideas to get the code to read line by line, not everything at once?

Thank you.

jacek
13th July 2006, 11:42
What characters are used to mark the end of line in those text files?

atm
13th July 2006, 15:33
I'm not sure. How do I figure that out?

jacek
13th July 2006, 15:44
I'm not sure. How do I figure that out?
The easiest way is to use hexeditor. CR is 0x0d and LF is 0x0a. Under Unix text files have only LF at the end of line and that's what QTextStream expects. Maybe those files were created on Mac (it uses CR, so it could fool QTextStream)?

atm
13th July 2006, 23:12
Thank you, that did the trick. I replaced all the 0ds with 0as in khexedit, works like a charm.