Trolltech Home | Qt-interest Home | Recent Threads | All Threads | Author | Date
All threads index page 5

Qt-interest Archive, March 2002
Read line of file to QString


Message 1 in thread

I want to read the data from a text file line by line. Each line of text will 
be stored in a QString. I will then store the list of strings in a 
QStringList.

How should I go about reading the file line by line and storing the text in a 
string?

Thanks, Mark


Message 2 in thread

Something like this:

QFile myFile("testFile"); // Create a file handle for the file named
"testFile"
QString line;

if (!myFile.open(IO_ReadOnly)) // Open the file
{
	// handle error
}

QTextStream stream( &myFile ); // Set the stream to read from myFile
line = stream.readLine(); // this reads a line (QString) from the file

Hope this helps.

You should check the doc for QTextStream and QFile, more there.

Erlend

> -----Original Message-----
> From: owner-qt-interest@trolltech.com
> [mailto:owner-qt-interest@trolltech.com]On Behalf Of Mark Finlay
> Sent: Wednesday, March 13, 2002 12:57
> To: qt-interest@trolltech.com
> Subject: Read line of file to QString
>
>
> I want to read the data from a text file line by line. Each line
> of text will
> be stored in a QString. I will then store the list of strings in a
> QStringList.
>
> How should I go about reading the file line by line and storing
> the text in a
> string?
>
> Thanks, Mark
>
> --
> List archive and information: http://qt-interest.trolltech.com
>
>


Message 3 in thread

Mark,

Try something like this...

  QFile file("MyFile.ext");
  QString line;
  if ( file.open(IO_ReadOnly | IO_Translate) ) {       
    // file opened successfully
    QTextStream t( &file );        // use a text stream
    // until end of file...
    while ( !t.eof() ) {           
      // read and parse the command line
      line = t.readLine();         // line of text excluding '\n'
      // do something with the line
      ...
    }
    // Close the file
    file.close();
  }


Mark McLaren

Mark Finlay wrote:
> 
> I want to read the data from a text file line by line. Each line of text will
> be stored in a QString. I will then store the list of strings in a
> QStringList.
> 
> How should I go about reading the file line by line and storing the text in a
> string?
> 
> Thanks, Mark
> 
> --
> List archive and information: http://qt-interest.trolltech.com


Message 4 in thread

> How should I go about reading the file line by line and 
> storing the text in a 
> string?

Open the file with QFile.
Create a QTextStream object with QFile object as the IODevice
and use QTextStream::readLine()
-- 
 [ signature omitted ]