Thread: fseek and stdin

  1. #1
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    fseek and stdin

    hi all,
    i read a snippet code and found he/she used
    fseek(stdin, 0, SEEK_END)
    to flush the "stdin", it works, but is it portable?

    thanks d'avance

  2. #2
    life is a nightmare
    Join Date
    Apr 2007
    Posts
    127
    i guess so. it's used for seeking the streams so i guess it's ok
    it's gonna seek to the bigging of stdin

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    The first google hit for "fseek stdin" indicates that it's not portable. http://lists.debian.org/debian-user/.../msg00389.html
    I tried to do a fseek(stdin , 0 , SEEK_END ) under linux.
    On windows and solaris this commands cleans the input stream. Under linux
    I get an error message il[l]egal seek.
    I may be wrong, but I remember reading something about using freopen() to open stdin for seeking. But that's probably just because freopen()ing stdin is just like fopen()ing a file.

    So, fseek()ing in stdin is probably not portable.

    [edit] If all you want to do is flush stdin, read this FAQ. http://faq.cprogramming.com/cgi-bin/...&id=1043284392 [/edit]
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    The call is portable.
    The effect you desire however might not be.

    fseek() has too many caveats (in the ISO standard), which when you add in various operating systems, and maybe redirecting stdin from say a file or a pipe, I doubt you'll get what you want in all cases.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  5. #5
    Registered User
    Join Date
    May 2007
    Location
    China
    Posts
    37

    Smile ok

    thanks guys

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. question about fseek
    By gp364481 in forum C Programming
    Replies: 6
    Last Post: 11-07-2008, 01:45 PM