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 overwritting a file?
Qt 6.11 is out! See what's new in the release blog

How to overwritting a file?

Scheduled Pinned Locked Moved General and Desktop
1 Posts 1 Posters 542 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.
  • V Offline
    V Offline
    VTiTux
    wrote on last edited by
    #1

    sorry... I said nothing.

    I had a question, but I understood my fault when I sent my message.
    It's my array that isn't emptied and which contains twice content. Sorry.
    How to remove this thread?


    Hi,

    I want to save my data into a text file, and I wrote this method:
    @
    bool JSONStorage::storeJSONFile(){
    if( !jsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate) ){
    return false;
    }
    QJsonDocument jsonDocument( jsonArray );
    jsonFile.write( jsonDocument.toJson() );
    jsonFile.close();
    return true;
    }@
    jsonFile is QFile.

    That works, but if I call this twice (later), my second content is appending at the end instead of overwriting my first content.
    Why?

    This code don't work too:
    @
    bool JSONStorage::storeJSONFile(){
    jsonFile.remove();
    if( !jsonFile.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Truncate) ){
    return false;
    }
    QJsonDocument jsonDocument( jsonArray );
    jsonFile.seek(0);
    jsonFile.write( jsonDocument.toJson() );
    jsonFile.resize(jsonFile.pos());
    jsonFile.flush();
    jsonFile.close();
    return true;
    }
    @

    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