Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [Solved] Problem with creating new File!!!
Forum Updated to NodeBB v4.3 + New Features

[Solved] Problem with creating new File!!!

Scheduled Pinned Locked Moved General and Desktop
10 Posts 6 Posters 19.2k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • P Offline
    P Offline
    Peggy
    wrote on last edited by
    #1

    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
    file.close();
    @

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

    ps: My operating system is Linux.

    1 Reply Last reply
    0
    • S Offline
      S Offline
      Sam
      wrote on last edited by
      #2

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

      1 Reply Last reply
      0
      • L Offline
        L Offline
        lgeyer
        wrote on last edited by
        #3

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

        1 Reply Last reply
        0
        • P Offline
          P Offline
          Peggy
          wrote on last edited by
          #4

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

          1 Reply Last reply
          0
          • F Offline
            F Offline
            felipe.c.sousa
            wrote on last edited by
            #5

            @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);
            arquivo.write(textoQueEuQueroSalvar.toLatin());
            arquivo.close();@

            From all, to all.

            1 Reply Last reply
            0
            • M Offline
              M Offline
              m.derempoukas
              wrote on last edited by
              #6

              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.

              1 Reply Last reply
              0
              • M Offline
                M Offline
                m.derempoukas
                wrote on last edited by
                #7

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

                @QFileInfo fi(file);
                QTextStream(stdout)<<fi.absoluteFilePath();@

                1 Reply Last reply
                0
                • F Offline
                  F Offline
                  felipe.c.sousa
                  wrote on last edited by
                  #8

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

                  From all, to all.

                  1 Reply Last reply
                  0
                  • P Offline
                    P Offline
                    Peggy
                    wrote on last edited by
                    #9

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

                    1 Reply Last reply
                    0
                    • V Offline
                      V Offline
                      vezprog
                      wrote on last edited by
                      #10

                      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
                      file.close();
                      return 0;
                      @

                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved