How to stop reading a file?

Discussion in 'C Programming' started by siliconwafer, Dec 30, 2005.

  1. siliconwafer

    siliconwafer Guest

    Hi All,
    I want to know tht how can one Stop reading a file in C (e.g a Hex
    file)with no 'EOF'?
    siliconwafer, Dec 30, 2005
    #1
    1. Advertising

  2. siliconwafer

    Arne Schmitz Guest

    siliconwafer wrote:

    > I want to know tht how can one Stop reading a file in C (e.g a Hex
    > file)with no 'EOF'?


    Look up feof().

    Arne

    --
    [--- PGP key FD05BED7 --- http://www.root42.de/ ---]
    Arne Schmitz, Dec 30, 2005
    #2
    1. Advertising

  3. siliconwafer

    Chuck F. Guest

    siliconwafer wrote:
    >
    > I want to know tht how can one Stop reading a file in C (e.g a
    > Hex file)with no 'EOF'?


    Where did you get such a big disk drive? Who put the endless file
    on it? What software did they use. Where did they get the data to
    write to the file?

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Dec 30, 2005
    #3
  4. On 30 Dec 2005 04:06:09 -0800, in comp.lang.c , "siliconwafer"
    <> wrote:

    >Hi All,
    >I want to know tht how can one Stop reading a file in C (e.g a Hex
    >file)with no 'EOF'?


    Stop calling fread() ? close the file? :)


    Seriously however, read up on feof().
    Mark McIntyre
    --

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 30, 2005
    #4
  5. "siliconwafer" <> writes:

    > Hi All,
    > I want to know tht how can one Stop reading a file in C (e.g a Hex
    > file)with no 'EOF'?


    Reading a file is not addictive, so it's just a matter of not
    reading any more from it.

    Btw, what do you mean with "with no EOF"? I can think of a few
    examples, like /dev/zero and /dev/random on unix systems, is that
    what you mean?

    /Niklas Norrthon
    Niklas Norrthon, Dec 30, 2005
    #5
  6. siliconwafer

    Eric Sosman Guest

    Chuck F. wrote:

    > siliconwafer wrote:
    >
    >>
    >> I want to know tht how can one Stop reading a file in C (e.g a
    >> Hex file)with no 'EOF'?

    >
    >
    > Where did you get such a big disk drive? Who put the endless file on
    > it? What software did they use. Where did they get the data to write
    > to the file?


    He's probably writing a quality-control test program
    to make sure /dev/zero contains only zeroes ;-)

    --
    Eric Sosman
    lid
    Eric Sosman, Dec 30, 2005
    #6
  7. siliconwafer

    SM Ryan Guest

    "siliconwafer" <> wrote:
    # Hi All,
    # I want to know tht how can one Stop reading a file in C (e.g a Hex
    # file)with no 'EOF'?

    The system should report to stdio the end of a disk file and stdio
    reports that with EOF or NULL returns or feof. For a file like a
    serial port or keyboard, you may have to define your own protocol
    within the file to work the end.

    --
    SM Ryan http://www.rawbw.com/~wyrmwif/
    I ASSURE YOU WE'RE OPEN!
    SM Ryan, Dec 30, 2005
    #7
  8. siliconwafer

    Afifov Guest

    can u give more details?
    Afifov, Dec 30, 2005
    #8
  9. siliconwafer

    Joe Wright Guest

    Afifov wrote:
    > can u give more details?
    >

    About what? When you read this group, do you pay attention?

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Dec 30, 2005
    #9
  10. siliconwafer

    Afifov Guest

    Dear Joe,

    I do read in this forum.But i believe that there is a couple of ways to go
    around reading a file without the use of eof, either by seeking to end of
    file through lseek, or getting size of the file and decrementing a counter
    (which would be initialized to file size) until it hits zero.
    It depends on the task.

    But thanks for the tip.
    Afifov, Dec 30, 2005
    #10
  11. "Afifov" <> writes:
    > can u give more details?


    Please do one of three things:

    1. Find out how to provide proper context in followups using whatever
    Usenet interface you're using. The way to do this for Google is
    described in detail at <http://cfaj.freeshell.org/google/>, but I have
    no idea how to do it through www.talkaboutprogramming.com.

    OR

    2. If that's not possible, switch to a different Usenet interface. A
    proper NNTP server would be ideal, but groups.google.com is free, and
    can be used properly with a little effort (see the URL above).

    OR

    3. Stop posting here.

    I'm not seriously suggesting alternative 3, but followups without
    context are quite annoying. Many of us do not have easy access to the
    parent article. Each followup should be readable by itself. I have
    no idea what you're asking for more details about; if you want
    information, it's *your* job to make yourself easy to understand.

    In addition to that, please write proper English (capitalize properly
    and don't use silly abbreviations like "u" for "you").

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 30, 2005
    #11
  12. siliconwafer

    Chuck F. Guest

    Afifov wrote:
    > can u give more details?
    >

    No. Mr. u is very sick. You will have to ask elsewhere. Can you
    provide proper context?

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Dec 30, 2005
    #12
  13. siliconwafer

    Joe Wright Guest

    Afifov wrote:
    > Dear Joe,
    >
    > I do read in this forum.But i believe that there is a couple of ways to go
    > around reading a file without the use of eof, either by seeking to end of
    > file through lseek, or getting size of the file and decrementing a counter
    > (which would be initialized to file size) until it hits zero.
    > It depends on the task.
    >
    > But thanks for the tip.
    >

    You missed my point. It was that you did not include any context in your
    post. If I can't see previous posts, I have no idea what you are talking
    about.

    --
    Joe Wright
    "Everything should be made as simple as possible, but not simpler."
    --- Albert Einstein ---
    Joe Wright, Dec 30, 2005
    #13
  14. siliconwafer said:

    > Hi All,
    > I want to know tht how can one Stop reading a file in C (e.g a Hex
    > file)with no 'EOF'?


    Keep reading until you receive an EOF message from your file-reading
    routine.

    For example:

    #include <stdio.h>
    int main(void)
    {
    int ch;
    while((ch = getchar()) != EOF)
    {
    putchar(ch);
    }
    return 0;
    }

    Of course, stdin is a great example of a file that doesn't have an EOF
    marker. So are most files, actually.

    You're probably thinking of the ^Z character (ASCII 26) that marks the end
    of some text files in CP/M and MS-DOS and Windows. C doesn't recognise this
    character as being particularly special (although your operating system
    might).

    C's EOF has nothing to do with all that, and is *not* a character. Rather,
    it's a message to your program that there's nothing left to read, which you
    get when you try to read past the end of the file.

    So - to read an entire file with no EOF character, just keep reading until
    you hit EOF.

    --
    Richard Heathfield
    "Usenet is a strange place" - dmr 29/7/1999
    http://www.cpax.org.uk
    email: rjh at above domain (but drop the www, obviously)
    Richard Heathfield, Dec 30, 2005
    #14
  15. "siliconwafer" <> writes:
    > I want to know tht how can one Stop reading a file in C (e.g a Hex
    > file)with no 'EOF'?


    The question you're asking doesn't make much sense, but I'm going to
    take a guess at what you actually meant.

    EOF is an abbreviation for End Of File. Every finite file has an end.
    (Some virtual files, such as /dev/zero and /dev/random on Unix-like
    systems, are infinite, but I don't think that's what you're referring
    to).

    EOF is a condition, not a character value. Some systems may use a
    special non-printable character to mark the end of a text file (e.g.,
    MS-DOS uses character 26, control-Z); this method can't work for a
    binary file that can contain any arbitrary character within it. If
    you're reading a text file, you'll never see this marker character;
    the C I/O functions will translate it to an EOF condition before you
    see it.

    You refer to a "Hex file". That would be a file containing a sequence
    of the printable characters '0'..'9' and 'A'..'F' (or 'a' .. 'f'), but
    I don't think that's what you mean. A binary file, which can contain
    any arbitrary data, is commonly *displayed* in hexadecimal, but it's
    not stored that way, so referring to a binary file as a "hex file" is
    incorrect.

    On some systems, you have to distinguish between text and binary modes
    when opening a file. On others, you don't have to (since text and
    binary files happen to behave the same way), but it's a good idea to
    do so anyway. Read your documentation for the fopen() function,
    particulary the meaning of the second argument ("mode").

    The way to read a single character from a file is to use the getc()
    function. If "in_file" is a FILE* you've opened with fopen(),
    you can call
    getc(in_file);
    and it will return an int value. If there's a character to be read,
    it will return the value of that character as an unsigned char
    converted to int; if your system has 8-bit characters (which it almost
    certainly does), this will be a value in the range 0..255. If you've
    reached the end of the file, getc() will return the special negative
    value EOF; since it's negative, it's distinct from any valid character
    value. (getc() also returns EOF if there's an error.)

    One common error is to store the value returned by getc() in a
    variable of type char rather than int. If you do this, you're
    throwing away information; for a binary file, some valid character
    values will look like EOF. Don't do this.

    Another common error is to use the feof() function rather than
    checking for EOF (a couple of people suggested this). The feof()
    function doesn't tell you that you've reached the end of the file; it
    tells you that you've just tried to go past the end of the file. The
    feof() function will return true only *after* getc() has returned EOF.
    Once getc() returns EOF, the feof() function can be useful for
    determining whether you've actually reached the end of the file or
    encountered an error; it's not a good way to determine whether there's
    anything left to read.

    The usual way to read a file (either text or binary) a character at a
    time is:

    int c;
    while ((c = getc(in_file) != EOF) {
    /* process a character */
    }

    The loop will terminate when you reach the end of the file.

    Read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

    Also, study this program:

    #include <stdio.h>
    #include <stdlib.h>
    #include <ctype.h>
    #include <errno.h>

    int main(int argc, char **argv)
    {
    FILE *in_file;
    int c;
    unsigned long count = 0;

    if (argc != 2) {
    fprintf(stderr, "Usage: %s filename\n", argv[0]);
    exit(EXIT_FAILURE);
    }

    in_file = fopen(argv[1], "rb");
    if (in_file == NULL) {
    perror(argv[1]);
    exit(EXIT_FAILURE);
    }

    while ((c = getc(in_file)) != EOF) {
    count ++;
    if (isprint(c)) {
    putchar(c);
    }
    else {
    switch (c) {
    case '\a':
    fputs("[\\a]", stdout);
    break;
    case '\b':
    fputs("[\\b]", stdout);
    break;
    case '\f':
    fputs("[\\f]", stdout);
    break;
    case '\n':
    fputs("[\\n]", stdout);
    putchar('\n');
    break;
    case '\r':
    fputs("[\\r]", stdout);
    break;
    case '\t':
    fputs("[\\t]", stdout);
    break;
    case '\v':
    fputs("[\\v]", stdout);
    break;
    default:
    printf("[%02X]", (unsigned)c);
    break;
    }
    }
    }

    printf("[EOF]\n\n");
    printf("Read %lu characters from %s\n", count, argv[1]);
    printf("feof(in_file) is %s\n", feof(in_file) ? "true" : "false");
    printf("ferror(in_file) is %s\n", ferror(in_file) ? "true" : "false");

    fclose(in_file);

    exit(EXIT_SUCCESS);
    }

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    We must do something. This is something. Therefore, we must do this.
    Keith Thompson, Dec 30, 2005
    #15
  16. siliconwafer

    Chuck F. Guest

    Afifov wrote:
    >
    > I do read in this forum.But i believe that there is a couple of
    > ways to go around reading a file without the use of eof, either
    > by seeking to end of file through lseek, or getting size of the
    > file and decrementing a counter (which would be initialized to
    > file size) until it hits zero. It depends on the task.


    However you consistently ignore the instructions given you on
    proper posting, with context. Since you refuse to cooperate, I
    think it is about time to start applying the PLONK cure, which
    means that people simply won't see, nor respond to, any of your posts.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Chuck F., Dec 30, 2005
    #16
  17. siliconwafer

    Al Balmer Guest

    On Fri, 30 Dec 2005 10:28:06 -0500, "Afifov" <> wrote:

    >can u give more details?


    Sure. About what?

    --
    Al Balmer
    Sun City, AZ
    Al Balmer, Dec 30, 2005
    #17
  18. On 30 Dec 2005 14:41:06 +0100, in comp.lang.c , Niklas Norrthon
    <> wrote:

    >Btw, what do you mean with "with no EOF"?


    He's probably reading a file and expecting to find an Ctrl-Z character
    in the stream, like one used to get with the old MSDOS text file
    structure. Crtl-Z seems to be called EOF in some character maps. Old
    DOS compilers would stop reading a text when they encountered a
    Ctrl-Z, even if the file table said the file had more data, and
    conversely wouldn't stop if the file didn't have a Crtl-Z, even if the
    'more data' was junk.
    Mark McIntyre
    --

    ----== Posted via Newsfeeds.Com - Unlimited-Unrestricted-Secure Usenet News==----
    http://www.newsfeeds.com The #1 Newsgroup Service in the World! 120,000+ Newsgroups
    ----= East and West-Coast Server Farms - Total Privacy via Encryption =----
    Mark McIntyre, Dec 30, 2005
    #18
  19. "Chuck F." wrote:
    >
    > Afifov wrote:
    > > can u give more details?
    > >

    > No. Mr. u is very sick. You will have to ask elsewhere. Can you
    > provide proper context?
    >

    u c m ducks? m r 2 ducks, c m wings???

    --
    +----------------------------------------------------------------+
    | Charles and Francis Richmond richmond at plano dot net |
    +----------------------------------------------------------------+
    Charles Richmond, Dec 31, 2005
    #19
  20. siliconwafer

    Lew Pitcher Guest

    -----BEGIN PGP SIGNED MESSAGE-----
    Hash: SHA1

    Charles Richmond wrote:
    > "Chuck F." wrote:
    >
    >>Afifov wrote:
    >>
    >>>can u give more details?
    >>>

    >>
    >>No. Mr. u is very sick. You will have to ask elsewhere. Can you
    >>provide proper context?
    >>

    >
    > u c m ducks? m r 2 ducks, c m wings???


    F U N E X ?
    s v f x.
    F U N E M?
    s v f m.
    O K. M N X.

    - --
    Lew Pitcher

    Master Codewright & JOAT-in-training | GPG public key available on request
    Registered Linux User #112576 (http://counter.li.org/)
    Slackware - Because I know what I'm doing.
    -----BEGIN PGP SIGNATURE-----
    Version: GnuPG v1.2.7 (GNU/Linux)

    iD8DBQFDtgNxagVFX4UWr64RAspCAKCvFktygVdv7anI1TYsP35gP/A78gCeOW1H
    Ka9N1jMiZhva+o5wXW04yBA=
    =kFvx
    -----END PGP SIGNATURE-----
    Lew Pitcher, Dec 31, 2005
    #20
    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. Matt Theule

    Stop Debugging doesn't stop in ASP.NET

    Matt Theule, Jul 23, 2003, in forum: ASP .Net
    Replies:
    7
    Views:
    732
    Matt Theule
    Jul 24, 2003
  2. Son KwonNam
    Replies:
    11
    Views:
    2,601
    mr_organic
    Apr 9, 2004
  3. Will
    Replies:
    1
    Views:
    15,232
    Thomas Weidenfeller
    Nov 2, 2004
  4. Mickey Segal
    Replies:
    24
    Views:
    775
    Sudsy
    Nov 9, 2004
  5. Angus
    Replies:
    5
    Views:
    451
    Ben Bacarisse
    Jul 18, 2010
Loading...

Share This Page