2010-11-11 93 views
3

我需要在txt文件的末尾寫入一些文本。但我只能重寫所有文件。我怎樣才能將文本添加到文件的末尾?qt寫入結束文件

謝謝。

回答

14

你確定你是在追加模式下打開文件嗎? QIODevice::Append

1

#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;} 

只需使用追加「a」標誌!

+0

但這不是QT ... – Mizmor 2013-07-23 21:06:21