CFile::Write 是 MFC(Microsoft Foundation Classes)中 CFile 类的一个公共方法,用于向文件写入数据。这个方法的声明如下:
UINT Write(
   const void* lpBuf,
   UINT nCount
);

其中,lpBuf 是一个指向要写入的数据的缓冲区的指针,nCount 是要写入的字节数。该方法返回实际写入的字节数。

以下是一个简单的示例代码,演示如何使用 CFile::Write 方法:
#include <afx.h>

int main() {
    // 假设有一个文件路径
    CString filePath = _T("C:\\Path\\To\\YourFile.txt");

    // 使用CFile打开文件
    CFile file;
    if (file.Open(filePath, CFile::modeReadWrite | CFile::modeCreate)) {
        // 要写入的数据
        const char* dataToWrite = "Hello, CFile!";

        // 写入数据
        UINT bytesWritten = file.Write(dataToWrite, strlen(dataToWrite));

        // 显示实际写入的字节数
        wprintf(L"实际写入的字节数:%u\n", bytesWritten);

        // 关闭文件
        file.Close();
    } else {
        // 文件打开失败的处理
        wprintf(L"无法打开文件。\n");
    }

    return 0;
}

请确保在项目中包含了 MFC 库,并根据实际情况修改文件路径。这个示例打开一个文件,写入指定的数据,并在控制台上显示实际写入的字节数。


转载请注明出处:http://www.pingtaimeng.com/article/detail/17715/MFC/CFile