April 25, 2012

Peggy Peggy
Lab Rat
25 posts

[Solved] Problem with creating new File!!!

 

Hello everyone,

It is almost about 2 days that I tried to create new text file in my project but it does not work!! I would like to create a textfile in my current project, which already is not existed.

Here is my code:

  1.     QFile file;
  2.     file.setFileName(file_name +".txt");//file_name is the QString, which I get as aparameter
  3.     file.open(QIODevice::ReadWrite | QIODevice::Text);
  4.     QTextStream stream(&file);
  5.     stream<<"Create Document";
  6.     QTextStream(stdout) << this->file_name; //To see the name of the file, which has just been typed
  7.     file.close();

I would be happy if someone could give me the tip.

ps: My operating system is Linux.

9 replies

April 25, 2012

Sam Sam
Area 51 Engineer
612 posts

Hi,
Your code is working!!! I am able to create a new file with the same code..!!! tested on windows.

April 25, 2012

Lukas Geyer Lukas Geyer
Gene Splicer
2074 posts

Are you sure that you have sufficient permissions?
What does QFile::error() state?

April 25, 2012

Peggy Peggy
Lab Rat
25 posts

First of all thanks for answers.

I added QFile::error() befoe file.close(). And I don’t see any error at console!!!!!

I would like to create the file in Linux so windows may not help me:(

April 25, 2012

felipe.c.sousa felipe.c.sou..
Ant Farmer
48 posts

  1. QString fileName =  QFileDialog(this,Qt::Widget).getSaveFileName(this,"Salvar Arquivo Txt",getenv("HOME"),"Arquivos de Texto(*.txt)");
  2. QFile arquivo(fileName);
  3. arquivo.open(QIODevice::WriteOnly | QIODevice::Text);
  4. arquivo.write(textoQueEuQueroSalvar.toLatin());
  5. arquivo.close();

 Signature 

From all, to all.

April 25, 2012

m.derempoukas m.derempoukas
Lab Rat
6 posts

Did you check if the file is created but in another directory than the one you expect it to be? Try passing an absolute path as parameter.

April 25, 2012

m.derempoukas m.derempoukas
Lab Rat
6 posts

On second thought add that , to check where do you actually save

  1. QFileInfo fi(file);
  2. QTextStream(stdout)<<fi.absoluteFilePath();

April 25, 2012

felipe.c.sousa felipe.c.sou..
Ant Farmer
48 posts

try use that to

  1. QString filename = QFileDialog::getSaveFileName(this,"Salvar relatório ...",diretorio+"\\Desktop","Arquivos Pdf(*.pdf)");

 Signature 

From all, to all.

April 26, 2012

Peggy Peggy
Lab Rat
25 posts

Hello all,

thanks for all the answers and especially thanks to “m.derempoukas”. I finally could track where my file has been saved and it was on build_desktop directory.

Now I am relieved:)

April 26, 2012

dvez43 dvez43
Ant Farmer
218 posts

suggestion, check the return from your QFile::open(). That can tell you if your file actually opened correctly…

  1. QFile file(QString(file_name).append(".txt"));
  2. qDebug() << "File location: " << QString(file_name).append(".txt");
  3. if (!file.open(QIODevice::ReadWrite | QIODevice::Text)){
  4.      qDebug() << "File open failed";
  5.      return -1;
  6. }        
  7. QTextStream stream(&file);
  8. stream << "Create Document";
  9. QTextStream(stdout) << this->file_name; //To see the name of the file, which has just been typed
  10. file.close();
  11. return 0;

 
  ‹‹ [SOLVED] Ok and Cancel buttons localization      When and how does the Qt assistant use libQtDbus? ››

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