简体   繁体   中英

qt write to end file

I need to write some text at the end of txt file. But i can only rewrite all file. How can i add text to the end of file?

Thank you.

Are you sure you are opening the file in the append mode? QIODevice::Append

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
int main(void)
{
    FILE *fp;
    size_t count;
    const char *str = "hello\n";

fp = fopen("yourFile.txt", "a");
if(fp == NULL) {
    perror("failed to open yourFile.txt");
    return EXIT_FAILURE;
}
count = fwrite(str, 1, strlen(str), fp);
printf("Wrote %u bytes. fclose(fp) %s.\n", count, fclose(fp) == 0 ? "succeeded" : "failed");return EXIT_SUCCESS;}

Just use the append "a" flag!

i did this after reading @Let_Me_Be 's answer

QString log;
for(int i=0;i<argc;i++){
    log+= argv[i];
    log+="\n";
}

QFile logFile("log.ini");
if(logFile.open(QIODevice::Append|QIODevice::Text)){
    QTextStream outLog(&logFile);
    outLog<<log;
}
logFile.close();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM