ReadFile question - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

ReadFile question

I have a question about ReadFile.
I open a file with CreateFile and then i want to read either character by character or line by line. With the fopen i can read the file either with a fgetc or fgets function, does the windows api have something like that ? Or must i read one byte at a time and then reposition the file pointer with SetFilePointer ? This latter sounds more complicated than what i really need to do which read one line at a time. Many thanks.

Comments

  • AsmGuru62AsmGuru62 Posts: 6,519Member
    : I have a question about ReadFile.
    : I open a file with CreateFile and then i want to read either character by character or line by line. With the fopen i can read the file either with a fgetc or fgets function, does the windows api have something like that ? Or must i read one byte at a time and then reposition the file pointer with SetFilePointer ? This latter sounds more complicated than what i really need to do which read one line at a time. Many thanks.
    :
    :
    [blue]You have a couple of options:

    1. (EASY) Use [b]fopen()[/b], [b]fgets()[/b] in [b]Win32[/b], just like in other apps.

    2. (HARDER) Code a class which encapsulates [b]CreateFile()[/b], [b]ReadFile()[/b] to use a buffer to read your lines into it.[/blue]
  • jtcoelhojtcoelho Posts: 44Member
    : : I have a question about ReadFile.
    : : I open a file with CreateFile and then i want to read either character by character or line by line. With the fopen i can read the file either with a fgetc or fgets function, does the windows api have something like that ? Or must i read one byte at a time and then reposition the file pointer with SetFilePointer ? This latter sounds more complicated than what i really need to do which read one line at a time. Many thanks.
    : :
    : :
    : [blue]You have a couple of options:
    :
    : 1. (EASY) Use [b]fopen()[/b], [b]fgets()[/b] in [b]Win32[/b], just like in other apps.
    :
    : 2. (HARDER) Code a class which encapsulates [b]CreateFile()[/b], [b]ReadFile()[/b] to use a buffer to read your lines into it.[/blue]
    :
    Okay, but i started the app using CreateFile to open the file and that's what i have used. I am not using C++, i guess i could, actually it
    would be fun to try, except that i have get this app out as soon as possible and i already started it using C only.
    I guess i could still use fopen although i had already opened it with CreateFile. My option for now is to
    just use the ReadFile, get a large buffer, since the line i need to read is not too far from the beginning of the file,
    and then parse the buffer with strtok and strstr functions. I was hoping that there was something in ReadFile
    that would let me read until the end of a line like fgets. Thanks for the suggestions.
  • jtcoelhojtcoelho Posts: 44Member
    : : I have a question about ReadFile.
    : : I open a file with CreateFile and then i want to read either character by character or line by line. With the fopen i can read the file either with a fgetc or fgets function, does the windows api have something like that ? Or must i read one byte at a time and then reposition the file pointer with SetFilePointer ? This latter sounds more complicated than what i really need to do which read one line at a time. Many thanks.
    : :
    : :
    : [blue]You have a couple of options:
    :
    : 1. (EASY) Use [b]fopen()[/b], [b]fgets()[/b] in [b]Win32[/b], just like in other apps.
    :
    : 2. (HARDER) Code a class which encapsulates [b]CreateFile()[/b], [b]ReadFile()[/b] to use a buffer to read your lines into it.[/blue]
    :
    Okay, but i started the app using CreateFile to open the file and that's what i have used. I am not using C++, i guess i could, actually it
    would be fun to try, except that i have get this app out as soon as possible and i already started it using C only.
    I guess i could still use fopen although i had already opened it with CreateFile. My option for now is to
    just use the ReadFile, get a large buffer, since the line i need to read is not too far from the beginning of the file,
    and then parse the buffer with strtok and strstr functions. I was hoping that there was something in ReadFile
    that would let me read until the end of a line like fgets. Thanks for the suggestions.
  • AsmGuru62AsmGuru62 Posts: 6,519Member
    [b][red]This message was edited by AsmGuru62 at 2003-1-20 8:4:35[/red][/b][hr]
    : : : I have a question about ReadFile.
    : : : I open a file with CreateFile and then i want to read either character by character or line by line. With the fopen i can read the file either with a fgetc or fgets function, does the windows api have something like that ? Or must i read one byte at a time and then reposition the file pointer with SetFilePointer ? This latter sounds more complicated than what i really need to do which read one line at a time. Many thanks.
    : : :
    : : :
    : : [blue]You have a couple of options:
    : :
    : : 1. (EASY) Use [b]fopen()[/b], [b]fgets()[/b] in [b]Win32[/b], just like in other apps.
    : :
    : : 2. (HARDER) Code a class which encapsulates [b]CreateFile()[/b], [b]ReadFile()[/b] to use a buffer to read your lines into it.[/blue]
    : :
    : Okay, but i started the app using CreateFile to open the file and that's what i have used. I am not using C++, i guess i could, actually it
    : would be fun to try, except that i have get this app out as soon as possible and i already started it using C only.
    : I guess i could still use fopen although i had already opened it with CreateFile. My option for now is to
    : just use the ReadFile, get a large buffer, since the line i need to read is not too far from the beginning of the file,
    : and then parse the buffer with strtok and strstr functions. I was hoping that there was something in ReadFile
    : that would let me read until the end of a line like fgets. Thanks for the suggestions.
    :
    [blue][b]ReadFile()[/b] will read data as binary (byte-by-byte) - it does not have the formatting option. If you want to read only one line and you know approx. offset of that line from the beginning of the file - then your larger buffer should work, but then your code will be not very flexible - if you need another line - you need to recode it again...

    The better solution will be just code a class (or [b]struct[/b] if you do it in C) which uses the Win32 API, but reads text files line-by-line. I did it once - not very hard. Just allocate a buffer and read symbols from that buffer keeping a count of remaining bytes - if it goes down to zero - read more from a file.[/blue]


Sign In or Register to comment.