How to overwritting a file?



  • 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;
    }
    @


Log in to reply
 

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