如何打开现有文件,以便在Visual Studion2010 C ++中附加新数据 [英] How do I open an existing file so new data is appended in Visual Studion2010 C++

查看:124
本文介绍了如何打开现有文件,以便在Visual Studion2010 C ++中附加新数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

使用下面列出的代码,新数据会覆盖现有数据。我想将新数据附加到文件中已有的数据



hFile = CreateFile((LPCTSTR)szSaveName,GENERIC_WRITE,0,NULL,

OPEN_EXISTING,FILE_APPEND_DATA,NULL);

bSuccess = WriteFile(hFile,sData,num,& written,NULL);



此致,Roland

With the code listed below, new data overwrites the existing data. I want to append the new data to that already in the file

hFile = CreateFile ((LPCTSTR) szSaveName, GENERIC_WRITE, 0, NULL,
OPEN_EXISTING, FILE_APPEND_DATA , NULL) ;
bSuccess=WriteFile(hFile,sData,num,&written,NULL);

Regards,Roland


推荐答案

看看MSDN:CreateFile功能



文档说FILE_APPEND_DATA应该进入第二个参数(dwDesiredAccess),并且在这种情况下不应该设置FILE_WRITE_DATA。



你试过吗?否则,我会选择KarstenK的解决方案。
Take a look at MSDN: CreateFile function.

The documentation says that the FILE_APPEND_DATA should go into the second parameter (dwDesiredAccess) and that FILE_WRITE_DATA should not be set in this case.

Have you tried that? Otherwise, I would go with the KarstenK's solution.


你需要函数 SetFilePointer

这是示例代码


为什么不使用(便携式)STL?只需创建一个具有读写权限的 fstream (这是默认设置),将write-cursor移动到缓冲区的末尾并开始写入。一个演示示例:



Why not just use the (portable) STL? Just create an fstream with read and write permissions (which is the default), move the write-cursor to the end of the buffer and start writing. A demonstrating example:

#include <iostream>
#include <fstream>
#include <sstream>
using namespace std;

int main()
{
    fstream file("somefile");
    file.seekp(0, ios_base::end);

    file << "\nadditional stuff";
}


这篇关于如何打开现有文件,以便在Visual Studion2010 C ++中附加新数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆