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.

6
  • Ugh. The MSVC CRT supports this. "ccs" in the fopen() mode argument, then just fgetws(). Commented Sep 1, 2010 at 2:13
  • Hans, MSVC CRT is NOT "native Win32". Commented Sep 1, 2010 at 2:34
  • 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. Commented Sep 1, 2010 at 2:56
  • @peachykeen, strtok() is part of MSVC CRT, not "native Win32". :) Commented Sep 1, 2010 at 3:01
  • @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. Commented Sep 1, 2010 at 4:30

2 Answers 2

4

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

Sign up to request clarification or add additional context in comments.

3 Comments

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.
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().
That apitalk.com link is dead... This one isn't, and it confirms your conclusion.
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);
}

2 Comments

What is the sleep for? and what is g_BytesTransferred?
@steve - sorry. My bad. Fixed

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.