August 8, 2011

MFreeM MFreeM
Lab Rat
12 posts

QFile read/write

 

Hi Guys,

I’m using QFile to create .bat files for mapping network drive/printers. This is going to be used for a support department so i want to try and make it as free from user error as possible.

My Program reads in a bat file and displays the content of the file in a TextEdit window. There is a problem, if the bat file doesn’t exist my program creates one, this is a default of QFile i think. How can i make it not create one? and do a test to confirm this?

  1.     QFile bat(finalpath); //finalpath is a QString containing the file path and name that i want to open to read
  2.     ui->batEdit->clear(); //batEdit is a textEdit object
  3.     bat.open(QIODevice::ReadOnly);
  4.     if(bat.open(QIODevice::ReadOnly))
  5.     {
  6.         QLabel* error = new QLabel("Cant Open file");
  7.         error->show();
  8.     }
  9.     QTextStream batStream (&bat);
  10.     QString line;
  11.     while(!batStream.atEnd())
  12.     {
  13.         line = batStream.readLine();
  14.         ui->batEdit->append(line);
  15.     }
  16.     bat.close();

Secondly,

After reading the file, i then want to either create the file(if it doesn’t exist) or append the file. There is a catch though, if one of the support engineers has misspelled the username, the folder structure then wouldnt exist. I dont want to create the folder structure with the QFile class, but i do want to create the file, if it doesn’t exist. Is that at all possible? Am i living in an idealistic world…

my code so far, below;

  1.     bat.open(QIODevice::ReadWrite | QIODevice::Append);
  2.     QString letter = ui->letterEdit->text();
  3.     QString share = ui->ShareEdit->text();
  4.     QString command = "net use ";
  5.     command = command+letter+" \\\\isi-itn\\"+share;
  6.     QTextStream fout(&bat);
  7.     fout << endl;
  8.     fout << command;
  9.     bat.close();

Oh, and in case you’re wondering at the code.. I’m a newbie. To C++(sort of) and Qt.

Thanks in advance

Michael

3 replies

August 8, 2011

ZapB ZapB
Ant Farmer
1427 posts

Check if the file exists first using QFileInfo::exists() [doc.qt.nokia.com].

 Signature 

Nokia Certified Qt Specialist
Interested in hearing about Qt related work

August 9, 2011

Volker Volker
Ant Farmer
5428 posts

open with QIODevice::ReadOnly does not create a file. The reason for the failure is that you reversed the logic. open returns true, in case it succeeded and returns fals in case it failed.

The following snippet prints “open FAILED” if the file does not exist:

  1. QFile f("/tmp/qdn-test.bat");
  2. if(f.open(QIODevice::ReadOnly))
  3.     qDebug() << "open ok";
  4. else
  5.     qDebug() << "open FAILED";

A better approach would be QFileInfo::exists(), as ZapB suggested.

And also make sure to abort the process in case the file does not exist. In your snippet, you just show the error message but continue in your workflow.

As a side note:
Don’t use a QLabel and show for your error message. Have a look at QMessageBox, especially at the static convenience methods to show a nice error box.

August 9, 2011

LinusA LinusA
Lab Rat
69 posts

Volker wrote:

As a side note:
Don’t use a QLabel and show for your error message. Have a look at QMessageBox, especially at the static convenience methods to show a nice error box.

Or QErrorMessage, while we’re at it :-)

 
  ‹‹ [SOLVED] Creation and writing to .dat file using QFile on Windows some problem...      Dynamic Qt Translation? ››

You must log in to post a reply. Not a member yet? Register here!