We help IT Professionals succeed at work.

error in runing read file programe

obad62
obad62 asked
on
Hi ,
I pout this code in Qt which reads file and I found  error I can't to solve it that are :

1- IO_ReadOnly was not declared in this scop
2- IO_Translate was not declared in this scop

how can I solve this problem and also I want to print result of reading on the consol how can I do that please ?
#include <QtGui/QApplication>
#include "mainwindow.h"
#include <QFile>
#include <QTextStream>
#include <QInputContext>
int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    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.atEnd() ) {
          // read and parse the command line
          line = t.readLine();         // line of text excluding '\n'
          // do something with the line
          //...
        }
        // Close the file
        file.close();
      }
    return a.exec();
}

Open in new window

Comment
Watch Question

Top Expert 2010
Commented:
try to change this line:

      if ( file.open(IO_ReadOnly | IO_Translate) ) {

to:

      if ( file.open( QIODevice::ReadOnly | QIODevice::Translate) ) {

Author

Commented:
Thank you ,the problem of error solved  but it still dos not read the file
Hi, IO_ReadOnly  and  IO_Translate are old Qt 3 defines whic are actually  QIODevice::ReadOnly in Qt4. So you should use  QIODevice::ReadOnly in spite of IO_ReadOnly. but just in case due to some legacy issue if you have to have that then you can #define IO_ReadOnly QIODevice::ReadOnly

I dont know about IO_Translate, but if this is meant for translating file in text mode then you can use The QIODevice::Text flag passed to open() which tells Qt to convert Windows-style line terminators ("\r\n") into C++-style terminators ("\n").
Here is little modified version of your code

#include <QtGui>

#define IO_ReadOnly QIODevice::ReadOnly 

int main(int argc, char *argv[])
{
    QApplication a(argc, argv);
    QFile file("main.cpp");
      QString line;
      if ( file.open(IO_ReadOnly ) ) {
        // file opened successfully
        QTextStream t( &file );        // use a text stream
        // until end of file...
        while ( !t.atEnd() ) {
          // read and parse the command line
          line = t.readLine();         // line of text excluding '\n'
		  qDebug()<<line;
          // do something with the line
          //...
        }
        // Close the file
        file.close();
      }
    return a.exec();
}

Open in new window