Reputation: 21817
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.
Upvotes: 5
Views: 7027
Reputation: 1198
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();
Upvotes: 0
Reputation: 3021
#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!
Upvotes: 1
Reputation: 36433
Are you sure you are opening the file in the append mode? QIODevice::Append
Upvotes: 16