Read text file (Unicode) in 'C' using native Win32
Asked Answered
C

2

6

I have a line-oriented text file (Unicode) that was created using CreateFile() and WriteFile().

Reading that file as a binary stream using ReadFile() is straightforward, but extra low-level processing is needed to break it into lines.

Is there a Win32 function that does this for me?

Again, please note that it's in 'C' (not C++) and I don't want to use POSIX/ANSI C functions such as readline().

If the answer to the aforementioned question is negative, what would be the "shortest code" to accomplish reading a line-oriented text file, using native Win32 C functions only? e.g. using ReadFile(), StrChr(), etc.

Thanks.

Calder answered 1/9, 2010 at 1:40 Comment(6)
Ugh. The MSVC CRT supports this. "ccs" in the fopen() mode argument, then just fgetws().Wolters
Hans, MSVC CRT is NOT "native Win32".Calder
Methinks shortest way would either be ReadFile in long chunks then strtok (preserving the last portion into the next read op) or ReadFile on single characters (2 bytes) until you reach an end-of-line sequence. I'm not sure enough to type a full answer with snippets, though.Staten
@peachykeen, strtok() is part of MSVC CRT, not "native Win32". :)Calder
@Android Eve: I coulda sworn it was in the STL and maybe even included in string.h. Even if it is CRT and can't be used, it provides the needed functionality and so could be re-written (or a non-CRT alternative found). :) My reasoning for suggesting it is that reading larger chunks with ReadFile is probably going to be faster than small chunks, but you may not be able to read the whole file and will need to be able to break the buffer into lines.Staten
Why the strict requirement about Win32 and not allowing the CRT (when you're using C, no less)?Polybasite
A
4

AFAIK there is no win32 function for reading file line by line.

Askari answered 1/9, 2010 at 1:45 Comment(3)
This is indeed what I have observed so far from examining: msdn.microsoft.com/en-us/library/aa364232%28VS.85%29.aspx But I may be missing something.Calder
The following great article shows unequivocally that there is no gets() equivalent in Win32: apitalk.com/windows-Programming/… So the next step for me is to create my own function to mimic that, using ReadFile() and StrChr().Calder
That apitalk.com link is dead... This one isn't, and it confirms your conclusion.Pitchfork
H
0

Here is a function skeleton that reads an entire file and supports 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;
    }
   
    if (dwBytesRead > 0 && dwBytesRead <= BUFFERSIZE-1)
    {
        ReadBuffer[dwBytesRead]=L'\0'; // NULL character

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

       
    CloseHandle(hFile);
}
Hermilahermina answered 30/9, 2019 at 2:17 Comment(2)
What is the sleep for? and what is g_BytesTransferred?Pooch
@Pooch - sorry. My bad. FixedHermilahermina

© 2022 - 2024 — McMap. All rights reserved.