Results 1 to 5 of 5

Thread: Reading File using QFile and QTextStream

  1. #1
    Join Date
    Jan 2006
    Location
    USA
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Question Reading File using QFile and QTextStream

    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.
    Last edited by jacek; 13th July 2006 at 11:39. Reason: post date has been changed to restore thread order

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading File using QFile and QTextStream

    What characters are used to mark the end of line in those text files?

  3. #3
    Join Date
    Jan 2006
    Location
    USA
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Default Re: Reading File using QFile and QTextStream

    I'm not sure. How do I figure that out?

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: Reading File using QFile and QTextStream

    Quote Originally Posted by atm
    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)?

  5. The following user says thank you to jacek for this useful post:

    atm (13th July 2006)

  6. #5
    Join Date
    Jan 2006
    Location
    USA
    Posts
    6
    Thanks
    2
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11

    Thumbs up Re: Reading File using QFile and QTextStream

    Thank you, that did the trick. I replaced all the 0ds with 0as in khexedit, works like a charm.

Similar Threads

  1. Qfile and QTextStream
    By sreedhar in forum Qt Programming
    Replies: 1
    Last Post: 28th June 2006, 10:43
  2. QSettings vs (QFile + Qtextstream)
    By nupul in forum Newbie
    Replies: 5
    Last Post: 10th April 2006, 07:26
  3. segfault on qtextstream
    By patcito in forum Qt Programming
    Replies: 13
    Last Post: 26th February 2006, 13:10
  4. create file in another directory
    By raphaelf in forum Qt Programming
    Replies: 3
    Last Post: 16th February 2006, 10:04

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.