Results 1 to 5 of 5

Thread: Reading File using QFile and QTextStream

Threaded View

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

    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

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
  •  
Qt is a trademark of The Qt Company.