gpt4 book ai didi

c++ - CreateFile/WriteFile 没有破坏旧文件的内容

转载 作者:塔克拉玛干 更新时间:2023-11-03 01:31:16 25 4
gpt4 key购买 nike

旧内容不会被清除。相反,数据正在被覆盖,所以我仍然看到旧内容。我没有做什么?

hFile = CreateFile(fname, // open testfile.txt
GENERIC_WRITE, // open for reading
0, // do not share
NULL, // default security
OPEN_ALWAYS, //
FILE_ATTRIBUTE_NORMAL, // normal file
NULL); // no attribute template

dwBytesToWrite = buff.GetLength();
WriteFile(hFile, buff.GetBuffer(100), dwBytesToWrite, &dwBytesWritten, NULL);

最佳答案

您为 dwCreationDisposition 指定了错误的值.您需要指定 CREATE_ALWAYS

Creates a new file, always. If the specified file exists and is writable, the function overwrites the file, the function succeeds, and last-error code is set to ERROR_ALREADY_EXISTS (183). If the specified file does not exist and is a valid path, a new file is created, the function succeeds, and the last-error code is set to zero.

关于c++ - CreateFile/WriteFile 没有破坏旧文件的内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8772559/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com