Move the file pointer to the beginning of the file (text mode inWindows)?

Discussion in 'C Programming' started by MBALOVER, Mar 17, 2010.

  1. MBALOVER

    MBALOVER Guest

    Hi all,

    I have a text file opened using the function fopen(filename,'r').
    Assume that after some file reading, the file pointer is at position
    X. Now I want to move it to the beginning of the file.

    I see function fsee() can help to move the file pointer to a position
    in the file. Unfortunately according to my book, on MS DOS, it works
    only with binary files. (in UNIX it works for both binary and text
    files, though).

    My program is written in Windows OS so I guess this function does not
    work either.

    Do you know in Windows, what function I can use to move the file
    pointer to a specific position in my text file?

    Thanks
    MBALOVER, Mar 17, 2010
    #1
    1. Advertising

  2. MBALOVER

    Seebs Guest

    Re: Move the file pointer to the beginning of the file (text mode in Windows)?

    On 2010-03-17, MBALOVER <> wrote:
    > I see function fsee() can help to move the file pointer to a position
    > in the file. Unfortunately according to my book, on MS DOS, it works
    > only with binary files. (in UNIX it works for both binary and text
    > files, though).
    >
    > My program is written in Windows OS so I guess this function does not
    > work either.
    >
    > Do you know in Windows, what function I can use to move the file
    > pointer to a specific position in my text file?


    This is arguably a Windows question. But!

    1. You can use fseek with a value previously returned by ftell. So
    if you call ftell at the start of your operation, you should be able to
    seek back to that location.
    2. rewind() should work, I believe.
    3. fseek(stream, 0, SEEK_SET) should work even on text streams.
    4. You probably need a better book.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Mar 17, 2010
    #2
    1. Advertising

  3. Re: Move the file pointer to the beginning of the file (text mode in

    In article <>, MBALOVER <> writes:
    > Hi all,
    >
    > I have a text file opened using the function fopen(filename,'r').
    > Assume that after some file reading, the file pointer is at position
    > X. Now I want to move it to the beginning of the file.


    rewind()


    > I see function fsee() can help to move the file pointer to a position
    > in the file. Unfortunately according to my book, on MS DOS, it works
    > only with binary files. (in UNIX it works for both binary and text
    > files, though).


    Seeking to offset 0 is much less generic than seeking to somewhere else,
    so it will work. The C99 standard says,

    ----v----
    The rewind function sets the file position indicator for the stream
    pointed to by stream to the beginning of the file. It is equivalent to

    (void)fseek(stream, 0L, SEEK_SET)

    except that the error indicator for the stream is also cleared.
    ----^----


    > Do you know in Windows, what function I can use to move the file
    > pointer to a specific position in my text file?


    This will always work:

    1) open the file
    2) read up to a point
    3) fgetpos()
    4) read on
    5) fsetpos() back to the position stored in step 3.

    lacos
    Ersek, Laszlo, Mar 17, 2010
    #3
  4. MBALOVER

    MBALOVER Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    Thanks Seebs and Laszlo,

    I will try these methods.

    By the way, the book I mentioned is "A Book on C: Programming in C
    (4th Edition)" by Kelley and Pohl.

    I am not sure if this is a good book but my colleague, a experienced
    software developer, recommended me this book.

    Thanks




    On Mar 16, 10:52 pm, (Ersek, Laszlo) wrote:
    > In article <..com>, MBALOVER <> writes:
    >
    > > Hi all,

    >
    > > I have a text file opened using the function fopen(filename,'r').
    > > Assume that after some file reading, the file pointer is at position
    > > X. Now I want to move it to the beginning of the file.

    >
    > rewind()
    >
    > > I see function fsee() can help to move the file pointer to a position
    > > in the file. Unfortunately according to my book, on MS DOS, it works
    > > only with binary files. (in UNIX it works for both binary and text
    > > files, though).

    >
    > Seeking to offset 0 is much less generic than seeking to somewhere else,
    > so it will work. The C99 standard says,
    >
    > ----v----
    > The rewind function sets the file position indicator for the stream
    > pointed to by stream to the beginning of the file. It is equivalent to
    >
    >         (void)fseek(stream, 0L, SEEK_SET)
    >
    > except that the error indicator for the stream is also cleared.
    > ----^----
    >
    > > Do you know in Windows, what function I can use to move the file
    > > pointer to a specific position in my text file?

    >
    > This will always work:
    >
    > 1) open the file
    > 2) read up to a point
    > 3) fgetpos()
    > 4) read on
    > 5) fsetpos() back to the position stored in step 3.
    >
    > lacos
    MBALOVER, Mar 17, 2010
    #4
  5. MBALOVER

    Ben Pfaff Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    (Ersek, Laszlo) writes:

    > Seeking to offset 0 is much less generic than seeking to somewhere else,
    > so it will work. The C99 standard says,
    >
    > ----v----
    > The rewind function sets the file position indicator for the stream
    > pointed to by stream to the beginning of the file. It is equivalent to
    >
    > (void)fseek(stream, 0L, SEEK_SET)
    >
    > except that the error indicator for the stream is also cleared.
    > ----^----


    More to the point:

    7.19.9.2 The fseek function
    Synopsis
    1 #include <stdio.h>
    int fseek(FILE *stream, long int offset, int whence);
    Description
    [...]
    4 For a text stream, either offset shall be zero, or offset
    shall be a value returned by an earlier successful call to
    the ftell function on a stream associated with the same file
    and whence shall be SEEK_SET.

    In other words, an offset of zero is explicitly allowed for a
    text stream.
    --
    char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    Ben Pfaff, Mar 17, 2010
    #5
  6. MBALOVER

    Seebs Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    On 2010-03-17, MBALOVER <> wrote:
    > I will try these methods.
    >
    > By the way, the book I mentioned is "A Book on C: Programming in C
    > (4th Edition)" by Kelley and Pohl.
    >
    > I am not sure if this is a good book but my colleague, a experienced
    > software developer, recommended me this book.


    Huh. I've heard good things about Kelley & Pohl before. Haven't read it
    myself, though.

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Mar 17, 2010
    #6
  7. MBALOVER

    MBALOVER Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    Thanks a lot.

    But I am still confused because what I found in the Kelley and Pohl's
    book:

    "The function fseek() and ftell() are guaranteed to work properly only
    on binary files. In MS-DOS, if we want to use these functions, the
    file should be opened with a binary mode."

    It would be great if everything is explained clearly. Why does the
    book say that? is it obsolete?

    Thanks




    On Mar 16, 11:09 pm, Ben Pfaff <> wrote:
    > (Ersek, Laszlo) writes:
    > > Seeking to offset 0 is much less generic than seeking to somewhere else,
    > > so it will work. The C99 standard says,

    >
    > > ----v----
    > > The rewind function sets the file position indicator for the stream
    > > pointed to by stream to the beginning of the file. It is equivalent to

    >
    > >         (void)fseek(stream, 0L, SEEK_SET)

    >
    > > except that the error indicator for the stream is also cleared.
    > > ----^----

    >
    > More to the point:
    >
    >      7.19.9.2 The fseek function
    >      Synopsis
    > 1           #include <stdio.h>
    >             int fseek(FILE *stream, long int offset, int whence);
    >      Description
    > [...]
    > 4    For a text stream, either offset shall be zero, or offset
    >      shall be a value returned by an earlier successful call to
    >      the ftell function on a stream associated with the same file
    >      and whence shall be SEEK_SET.
    >
    > In other words, an offset of zero is explicitly allowed for a
    > text stream.
    > --
    > char a[]="\n .CJacehknorstu";int putchar(int);int main(void){unsigned long b[]
    > ={0x67dffdff,0x9aa9aa6a,0xa77ffda9,0x7da6aa6a,0xa67f6aaa,0xaa9aa9f6,0x11f6},*p
    > =b,i=24;for(;p+=!*p;*p/=4)switch(0[p]&3)case 0:{return 0;for(p--;i--;i--)case+
    > 2:{i++;if(i)break;else default:continue;if(0)case 1:putchar(a[i&15]);break;}}}
    MBALOVER, Mar 17, 2010
    #7
  8. MBALOVER

    Seebs Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    On 2010-03-17, MBALOVER <> wrote:
    > But I am still confused because what I found in the Kelley and Pohl's
    > book:
    >
    > "The function fseek() and ftell() are guaranteed to work properly only
    > on binary files. In MS-DOS, if we want to use these functions, the
    > file should be opened with a binary mode."
    >
    > It would be great if everything is explained clearly. Why does the
    > book say that? is it obsolete?


    That sounds like a plain error. However, since the behavior of fseek()
    and ftell() is a bit weird on text files, I could easily imagine someone
    getting the impression that they just don't work there. Still, either
    it's a plain error, or MS-DOS compilers and libraries were crap.
    (Note that Windows is not actually MS-DOS, although they have some
    similarities.)

    The basic issue, as it would apply to DOS, is this: Imagine that you are
    reading a text file. Somewhere in there, there will be a \r\n sequence,
    perhaps.

    If you call getc() while the stream is pointing at the \r, it'll consume
    both characters and return a \n, in text mode. Now what happens if, after
    that, you seek back a single character? It's not a valid state for the
    stream. There's no such thing as a bare newline in a proper text file.

    So in general, it's not defined to seek to arbitrary locations -- but it is
    defined to seek to zero, or to a value returned by ftell().

    -s
    --
    Copyright 2010, all wrongs reversed. Peter Seebach /
    http://www.seebs.net/log/ <-- lawsuits, religion, and funny pictures
    http://en.wikipedia.org/wiki/Fair_Game_(Scientology) <-- get educated!
    Seebs, Mar 17, 2010
    #8
  9. MBALOVER

    MBALOVER Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    On Mar 16, 11:47 pm, Seebs <> wrote:
    > That sounds like a plain error.  However, since the behavior of fseek()
    > and ftell() is a bit weird on text files, I could easily imagine someone
    > getting the impression that they just don't work there.  Still, either
    > it's a plain error, or MS-DOS compilers and libraries were crap.
    > (Note that Windows is not actually MS-DOS, although they have some
    > similarities.)
    >
    > The basic issue, as it would apply to DOS, is this:  Imagine that you are
    > reading a text file.  Somewhere in there, there will be a \r\n sequence,
    > perhaps.
    >
    > If you call getc() while the stream is pointing at the \r, it'll consume
    > both characters and return a \n, in text mode.  Now what happens if, after
    > that, you seek back a single character?  It's not a valid state for the
    > stream.  There's no such thing as a bare newline in a proper text file.
    >
    > So in general, it's not defined to seek to arbitrary locations -- but it is
    > defined to seek to zero, or to a value returned by ftell().
    >




    Hi Seebs,

    Your explanation is clear and makes sense to me.

    Thanks a lot.

    Best,
    MBALOVER, Mar 17, 2010
    #9
  10. MBALOVER

    Richard Bos Guest

    Re: Move the file pointer to the beginning of the file (text mode in

    Seebs <> wrote:

    > On 2010-03-17, MBALOVER <> wrote:
    > > But I am still confused because what I found in the Kelley and Pohl's
    > > book:
    > >
    > > "The function fseek() and ftell() are guaranteed to work properly only
    > > on binary files. In MS-DOS, if we want to use these functions, the
    > > file should be opened with a binary mode."
    > >
    > > It would be great if everything is explained clearly. Why does the
    > > book say that? is it obsolete?

    >
    > That sounds like a plain error. However, since the behavior of fseek()
    > and ftell() is a bit weird on text files, I could easily imagine someone
    > getting the impression that they just don't work there. Still, either
    > it's a plain error, or MS-DOS compilers and libraries were crap.


    I still have a book on Turbo C lying about, and it does mention 0 for
    text files. So it's an error in K&P.

    Richard
    Richard Bos, Mar 17, 2010
    #10
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ryniek90
    Replies:
    0
    Views:
    537
    Ryniek90
    Aug 24, 2009
  2. Daku
    Replies:
    3
    Views:
    370
    kopik
    Dec 31, 2009
  3. Aleksey
    Replies:
    1
    Views:
    308
    Nobody
    Jan 23, 2010
  4. Alf P. Steinbach
    Replies:
    11
    Views:
    516
    Tobias Müller
    Nov 26, 2011
  5. Jesse B.
    Replies:
    9
    Views:
    214
    Jesse B.
    Mar 27, 2010
Loading...

Share This Page