【问题标题】:How to write ASCII string to file using Windows API WriteFile?如何使用 Windows API WriteFile 将 ASCII 字符串写入文件?
【发布时间】:2012-11-12 05:36:57
【问题描述】:

我知道我们可以使用WriteFile 向文件写入内容:

BOOL WINAPI WriteFile (HANDLE hFile, 
                       LPCVOID lpBuffer, 
                       DWORD numOfBytesToWrite,
                       LPDOWRD lpNumOfBytesWritten,
                       LPOVERLAPPED lpOverlapped);

但是我们如何使用这个函数将 ASCII 字符串写入文件呢?

【问题讨论】:

  • 它写入“字节缓冲区”(通过lpBuffer 指定)。谢天谢地,ASCII 很好地映射到字节..

标签: c++ c windows file-io


【解决方案1】:

通常是这样的:

WriteFile(your_file, your_string, strlen(your_string), &written, NULL);

你通常不想这样做——除非你的字符串很长,如果你使用像fprintffputs这样的东西来利用它几乎总是更有效(即更快)标准库中内置的缓冲。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2011-11-21
    • 2016-09-28
    • 2013-05-13
    • 2011-02-13
    • 2019-05-18
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多