6

我有一个使用 CreateFile() 和 WriteFile() 创建的面向行的文本文件 (Unicode)。

使用 ReadFile() 将该文件作为二进制流读取很简单,但需要额外的低级处理才能将其分成几行。

是否有一个 Win32 函数可以为我执行此操作?

再次请注意,它在“C”(不是 C++)中,我不想使用诸如 readline() 之类的 POSIX/ANSI C 函数。

如果上述问题的答案是否定的,那么仅使用本机 Win32 C 函数来完成读取面向行的文本文件的“最短代码”是什么?例如使用 ReadFile()、StrChr() 等。

谢谢。

4

2 回答 2

4

AFAIK没有用于逐行读取文件的win32函数。

于 2010-09-01T01:45:57.723 回答
0

这是一个读取整个文件并支持 UNICODE 的函数框架:

  void MyReadFile(wchar_t *filename)
  {

    HANDLE hFile; 
    DWORD  dwBytesRead = 0;
    wchar_t   ReadBuffer[BUFFERSIZE] = {0};
    OVERLAPPED ol = {0};


    hFile = CreateFile(filename,
                       GENERIC_READ,          // open for reading
                       FILE_SHARE_READ,       // share for reading
                       NULL,                  // default security
                       OPEN_EXISTING,         // existing file only
                       FILE_ATTRIBUTE_NORMAL | FILE_FLAG_OVERLAPPED, // normal file
                       NULL);                 // no attr. template

    if (hFile == INVALID_HANDLE_VALUE) 
    { 

        return; 
    }

    // Read one character less than the buffer size to save room for
    // the terminating NULL character. 

    if( ReadFileEx(hFile, ReadBuffer, BUFFERSIZE-1, &ol, FileIOCompletionRoutine) == FALSE)
    {

        CloseHandle(hFile);
        return;
    }
    SleepEx(5000, TRUE);
    dwBytesRead = g_BytesTransferred;

    if (dwBytesRead > 0 && dwBytesRead <= BUFFERSIZE-1)
    {
        ReadBuffer[dwBytesRead]=L'\0'; // NULL character

    }
    else if (dwBytesRead == 0)
    {
    }
    else
    {
    }


    CloseHandle(hFile);
}
于 2019-09-30T02:17:49.173 回答