WriteFile 随机读写文件,怎么做?

bacy001 2011-04-01 08:43:10
我现在的问题是:每次打开文件都是从头开始写文件(导致老信息被覆盖),而我希望是“打开一个文件,如果存在,新内容添加在文件尾部;如果文件不存在,则创建文件!

	HANDLE m_hLogFile = CreateFile(fname,
GENERIC_WRITE,
FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);

	WriteFile(m_hLogFile,buf,lstrlen(buf),&num,NULL);


使用了 SetEndOfFile,也无效!

那位帅哥有经验的,分享下...
...全文
220 12 打赏 收藏 转发到动态 举报
写回复
用AI写文章
12 条回复
切换为时间正序
请发表友善的回复…
发表回复
Lactoferrin 2011-04-02
  • 打赏
  • 举报
回复
HANDLE m_hLogFile = CreateFile(fname,
FILE_APPEND_DATA|SYNCHRONIZE,
FILE_SHARE_READ,
&sa,
OPEN_ALWAYS,
FILE_ATTRIBUTE_NORMAL,
NULL);
  • 打赏
  • 举报
回复
那就用 fopen(, "ab");
wyx100 2011-04-02
  • 打赏
  • 举报
回复
CreateFile(xxxx,FILE_FLAG_OVERLAPPED,NULL);


m_Ol.hEvent = CreateEvent(xxxx);

WriteFile(xxxx&m_Ol); //开始写入数据

读就不用说了把
lslsyqyq 2011-04-02
  • 打赏
  • 举报
回复
用 OVERLAPPED 的话
写文件 就要设置OVERLAPPED
麻烦的
直接定位到文件末尾多方便
yiruirui0507 2011-04-02
  • 打赏
  • 举报
回复
This function moves the file pointer of an open file. A RAPI version of this function exists, called CeSetFilePointer (RAPI).

CopyDWORD SetFilePointer(
HANDLE hFile,
LONG lDistanceToMove,
PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod
);

Parameters
hFile
[in] Handle to the file whose file pointer is to be moved. The file handle must have been created with GENERIC_READ or GENERIC_WRITE access to the file.
lDistanceToMove
Low-order 32 bits of a signed value that specifies the number of bytes to move the file pointer. A positive value for lDistanceToMove moves the file pointer forward in the file, and a negative value moves the file pointer backward. Note that you cannot use a negative value to move back past beyond the beginning of a file.
lpDistanceToMoveHigh
Not supported; must be NULL or point to a value of zero.
dwMoveMethod
[in] Starting point for the file pointer move. The following table shows possible values for this parameter.
Value Description
FILE_BEGIN Indicates that the starting point is zero or the beginning of the file.
FILE_CURRENT Indicates that the starting point is the current value of the file pointer.
FILE_END Indicates that the starting point is the current EOF position.
yiruirui0507 2011-04-02
  • 打赏
  • 举报
回复
SetFilePointer直接将指针定制到文件末尾
xyy410874116 2011-04-02
  • 打赏
  • 举报
回复
fOpen()
anbam 2011-04-02
  • 打赏
  • 举报
回复
fopen_s( &pFile, ,"a+")
xengine-qyt 2011-04-01
  • 打赏
  • 举报
回复
CreateFile(xxxx,FILE_FLAG_OVERLAPPED,NULL);


m_Ol.hEvent = CreateEvent(xxxx);

WriteFile(xxxx&m_Ol); //开始写入数据

读就不用说了把
bacy001 2011-04-01
  • 打赏
  • 举报
回复
如果用 OVERLAPPED 如果实现呢?
lazy_2010 2011-04-01
  • 打赏
  • 举报
回复
就是这样用的啊,你打开已经存在的文件之后,调用 SetFilePointerEx 了么?
Dreadnought 2011-04-01
  • 打赏
  • 举报
回复
SetFilePointer

16,472

社区成员

发帖
与我相关
我的任务
社区描述
VC/MFC相关问题讨论
社区管理员
  • 基础类社区
  • Web++
  • encoderlee
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告

        VC/MFC社区版块或许是CSDN最“古老”的版块了,记忆之中,与CSDN的年龄几乎差不多。随着时间的推移,MFC技术渐渐的偏离了开发主流,若干年之后的今天,当我们面对着微软的这个经典之笔,内心充满着敬意,那些曾经的记忆,可以说代表着二十年前曾经的辉煌……
        向经典致敬,或许是老一代程序员内心里面难以释怀的感受。互联网大行其道的今天,我们期待着MFC技术能够恢复其曾经的辉煌,或许这个期待会永远成为一种“梦想”,或许一切皆有可能……
        我们希望这个版块可以很好的适配Web时代,期待更好的互联网技术能够使得MFC技术框架得以重现活力,……

试试用AI创作助手写篇文章吧