CreateFile/WriteFile不会破坏旧文件的内容

jdl*_*jdl 4 c++ visual-c++

旧的内容没有被消灭.相反,数据被写入,所以我仍然看到旧内容.我没做什么?

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);
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 5

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

始终创建新文件.如果指定的文件存在且可写,则该函数将覆盖该文件,该函数成功,并将最后一个错误代码设置为ERROR_ALREADY_EXISTS(183).如果指定的文件不存在且是有效路径,则创建新文件,函数成功,最后错误代码设置为零.