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. How to open file after saving?
Qt 6.11 is out! See what's new in the release blog

How to open file after saving?

Scheduled Pinned Locked Moved General and Desktop
8 Posts 4 Posters 3.0k 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.
  • L Offline
    L Offline
    LeeMinh
    wrote on last edited by
    #1

    Hi,

    I am writing a demo application to save data to *.txt file. And this is the code I used

    @
    QString fileName = QFileDialog::getSaveFileName(
    this,
    tr("Save As"),
    "",
    tr("Normal text file (.txt);;All Files (.*)"));

    if (fileName.isEmpty()) return;
    else {
    QFile file(fileName);
    if (!file.open(QIODevice::WriteOnly)) {
    QMessageBox::information(this, tr("Unable to open file"), file.errorString());
    return;
    }

    QTextStream stream(&file);

    // Print data
    stream << "Hello World";

    stream.flush();
    file.close();

    // Open the file after writing data
    file.open(QIODevice::ReadOnly);
    }
    @

    And now I want to open the text file that I have just saved. And I tried this code

    @file.open(QIODevice::ReadOnly)@

    But the file is not opened. How can I do it?

    Thanks!

    1 Reply Last reply
    0
    • JeroentjehomeJ Offline
      JeroentjehomeJ Offline
      Jeroentjehome
      wrote on last edited by
      #2

      Where in your code did you place the code?
      Just a thought, but open the file in readWrite mode. Then you can write the data, read the data and close the file.
      Btw, it's bad practive to have multiple return statements in a function. this to avoid confusion to other readers. Maybe they are logic, but still, confusing.

      Greetz, Jeroen

      1 Reply Last reply
      0
      • raven-worxR Offline
        raven-worxR Offline
        raven-worx
        Moderators
        wrote on last edited by
        #3

        and whats the error message in the case it's not opened?

        --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
        If you have a question please use the forum so others can benefit from the solution in the future

        1 Reply Last reply
        0
        • JeroentjehomeJ Offline
          JeroentjehomeJ Offline
          Jeroentjehome
          wrote on last edited by
          #4

          And, how do you get the idea that the file is not opened?
          When you write to a file, the file will be at it's end. To read the begin again, you need to 'reset' the file position it's reading. Checkout QFile to do so.

          Greetz, Jeroen

          1 Reply Last reply
          0
          • L Offline
            L Offline
            LeeMinh
            wrote on last edited by
            #5

            I have edited the code and you can see it. Thanks for your advice!
            [quote author="Jeroentje@home" date="1380782812"]Where in your code did you place the code?
            Just a thought, but open the file in readWrite mode. Then you can write the data, read the data and close the file.
            Btw, it's bad practive to have multiple return statements in a function. this to avoid confusion to other readers. Maybe they are logic, but still, confusing. [/quote]

            1 Reply Last reply
            0
            • L Offline
              L Offline
              LeeMinh
              wrote on last edited by
              #6

              I don't get any error messages in the case it's not opened.

              In my application, I have a button that allows users to export data to text file (*.txt). After they choose the file path and save it, the application will write data into that file and at last the application will automatically open the file that they have just saved to view the result.

              [quote author="raven-worx" date="1380782837"]and whats the error message in the case it's not opened? [/quote]

              [quote author="Jeroentje@home" date="1380782901"]And, how do you get the idea that the file is not opened?
              When you write to a file, the file will be at it's end. To read the begin again, you need to 'reset' the file position it's reading. Checkout QFile to do so.[/quote]

              1 Reply Last reply
              0
              • SGaistS Offline
                SGaistS Offline
                SGaist
                Lifetime Qt Champion
                wrote on last edited by
                #7

                Hi,

                When opening again you don't test if the open succeeded, so how can you be sure it failed ?

                Also, you didn't show the code you use to read the file

                Interested in AI ? www.idiap.ch
                Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

                1 Reply Last reply
                0
                • raven-worxR Offline
                  raven-worxR Offline
                  raven-worx
                  Moderators
                  wrote on last edited by
                  #8

                  [quote author="LeeMinh" date="1380784725"]I don't get any error messages in the case it's not opened.[/quote]
                  so "file.errorString()" returns an empty string?!
                  How do you really know that the opening failed?

                  --- SUPPORT REQUESTS VIA CHAT WILL BE IGNORED ---
                  If you have a question please use the forum so others can benefit from the solution in the future

                  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