2010-11-11 87 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