Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

qt write to end file

Tags:

c++

qt

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.

like image 792
0xAX Avatar asked Nov 11 '10 14:11

0xAX


2 Answers

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

like image 141
Šimon Tóth Avatar answered Nov 07 '22 15:11

Šimon Tóth


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

like image 31
dirbacke Avatar answered Nov 07 '22 16:11

dirbacke