An fgets() implementation using ReadFile()

This is a discussion on An fgets() implementation using ReadFile() within the Windows Programming forums, part of the Platform Specific Boards category; I'm trying to write a function that works the same as fgets() (reading a file/stream line-by-line) except using ReadFile. By ...

  1. #1
    Registered User
    Join Date
    Oct 2004
    Posts
    1

    An fgets() implementation using ReadFile()

    I'm trying to write a function that works the same as fgets() (reading a file/stream line-by-line) except using ReadFile. By doing so, I'm able to use it with CreateFile/LockFile/etc. and other similar WinAPI HANDLE-based functions to avoid having to additionally use fopen() and fgets().

    I have come up with two methods so far, though I am not sure if either of the two are the ideal solution. I'm hoping someone on this forum can shed an idea/example or two.

    Method 1: Reading the file char-by-char until the EOL (end of line)
    - Pros: You are not reading any unnecessary data.
    - Cons: Extremely slow, since you are reading only one character at a time.
    - I have tested this method, and it proved to be quite slow.

    Method 2: Reading a fixed buffer size until the EOL
    - If the buffer has read more then the EOL position, I would then use SetFilePointer() to set move the current file pointer to after the EOL for the next call.
    - Pros: Most likely faster (not sure).
    - Cons: You are reading unnecessary data from the file since not every line would be the same as your fixed buffer.
    - I have not tested this method yet.

    Any comments or suggestions are greatly appreciated.

  2. #2
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,859
    I avoid multiple reads as file I/O is very slow.

    Usually I find the file size and read it all into a dynamically allocated buffer. Then work with the buffer as needed using pointers.

    There may be reasons why this approach is impractical but it avoids many issues.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. fgets not working after fgetc
    By 1978Corvette in forum C Programming
    Replies: 3
    Last Post: 01-22-2006, 06:33 PM
  2. problem with fgets
    By learninC in forum C Programming
    Replies: 3
    Last Post: 05-19-2005, 09:10 AM
  3. problem with fgets
    By Smoot in forum C Programming
    Replies: 4
    Last Post: 12-07-2003, 03:35 AM
  4. fgets crashing my program
    By EvBladeRunnervE in forum C++ Programming
    Replies: 7
    Last Post: 08-11-2003, 01:08 PM
  5. help with fgets
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-17-2001, 09:18 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21