How to open file after saving?



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



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


  • Moderators

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



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



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



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


  • Lifetime Qt Champion

    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


  • Moderators

    [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?


Log in to reply
 

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