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

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

    ps: My operating system is Linux.

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

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

  • 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:(

  • @QString fileName = QFileDialog(this,Qt::Widget).getSaveFileName(this,"Salvar Arquivo Txt",getenv("HOME"),"Arquivos de Texto(*.txt)");
    QFile arquivo(fileName);
    arquivo.open(QIODevice::WriteOnly | QIODevice::Text);

  • 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.

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

    @QFileInfo fi(file);

  • try use that to
    @QString filename = QFileDialog::getSaveFileName(this,"Salvar relatório ...",diretorio+"\Desktop","Arquivos Pdf(*.pdf)");@

  • 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:)

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

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

Log in to reply

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.