How do reading functions mark an end?

This is a discussion on How do reading functions mark an end? within the C Programming forums, part of the General Programming Boards category; Hi all, How does a reading function , eg scanf() or fgets(), mark the end of an input string(character)? with ...

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

    How do reading functions mark an end?

    Hi all,
    How does a reading function , eg scanf() or fgets(), mark the end of an input string(character)? with EOF , NULL ,'\0' , or '\n'?
    Take a look at these these snippets, do they want to test the end of a stream? but in vc, NULL is defined as 0, and EOF is -1.......headache....

    Code:
    if (fgets(buf, sizeof(buf), stdin) != NULL)
    Code:
    while ((c = fgetc(fp)) != EOF)
    Code:
    while (!feof(fp))
    so, how should i test the end of a stream?

    any advice would be appreciated! thanks!

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    Much clearer, and more....

    Thank you , it is much better now,
    Can I understand it this way?
    1.EOF is a negative value returned only by a read function.
    2.'\0' is set after the last character to mark the end of a string. so we use '\0' to determine
    the end of a stream.
    3.NULL is always recommanded to be used for pointers.
    and NULL is always a return code of a function.
    4. NULL isn't equal to '\0'.
    is it right?

    and more questions..
    a pice of orginal text from "Why it's bad to use feof() to control a loop"
    Code:
    most read functions will set EOF once they've read all the data, and then performed a final
    read resulting in no data, only EOF
    I am a little confusing about it,
    Code:
    most read functions will set EOF once they've read all the data,
    set EOF means set '/0'?
    Code:
    and then performed a final read resulting in no data, only EOF
    after haveing set EOF, why they read for a final one, to verify that all data was read?

    Code:
    i = 0;
    while (!feof(fp))
    {
      fgets(buf, sizeof(buf), fp);
      printf ("Line %4d: %s", i, buf);
      i++;
    }
    Code:
    as the program goes through the loop to get the last line of data, fgets() works normally,
    without setting EOF .
    as mentioned above, it should set EOF right after getting all data,why here it goes
    without setting EOF?
    Code:
     and we print out the data. The loop returns to the
    top, and the call to feof() returns FALSE, and we start to go through the loop again. This
    time, the fgets() sees and sets EOF
    why now (set EOF)?
    Code:
     
    but thanks to our poor logic, we go on to
    process the buffer anyway, without realising that its content is now undefined (most likely
    untouched from the last loop).
    looking forward answers, thanks a lot

  4. #4
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    EOF is a constant - it means End Of File. On most systems its defined as -1.

    2. No we don't use \0 to determine the end of a stream, use NULL (\0) to determine the end of a string
    3. Yes.
    4. NULL = 0 = 0x00 = '\0' (on most systems)

    For example, to read chars until the end of a stream:

    Code:
    int cc;
    
    while((cc = fgetc(stream)) != EOF)
        putchar(cc);
    Last edited by zacs7; 05-13-2007 at 06:46 AM.

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

    ~

    Code:
    2. No we don't use \0 to determine the end of a stream, use NULL (\0) to
    determine the end of a string
    you mean that we compare the return code of a read function with NULL to determine the end of sting?
    or
    you mean that we should use the return code from the read function to determine the end of string?
    eg..
    Code:
     while (fgets(buf, sizeof(buf), stream) != NULL)
     //fgets returns null when met end.
     //here, can we use '\0' instead of NULL, though as you said : No we don't use
     //'\0' to determine the end of a stream, use NULL (\0) to determine the end of
     //a string.......(i messed...)
    
    ........
    Code:
    int cc;
    while((cc = fgetc(stream)) != EOF)
     //fgetc returns EOF when met end.
    .....
    right?

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    you mean that we compare the return code of a read function with NULL to determine the end of sting?
    or
    you mean that we should use the return code from the read function to determine the end of string?
    No, end of a string and end of a stream are two different things.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    oh?

    when we use
    Code:
    fgets(buf, sizeof(buf), stdin)
    what it get from stdin is a stream or a string?
    what stored in buf is a string?

    Code:
    int cc;
    while((cc = fgetc(stream)) != EOF)
        putchar(cc);
    here can stream be stdin? or it must be a file, or like....char buf[10]

  8. #8
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    All the 'f' file functions work just as well with stdin, stdout as they do with files opened with fopen.

    So it's very easy to move from
    fgets(buf, sizeof(buf), stdin)

    to
    fgets(buf, sizeof(buf), fp)
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

    what about stream and string?

    what about stream and string?

    when we use
    Code:
    fgets(buf, sizeof(buf), stdin);
    what it get from stdin is a stream or a string?
    what stored in buf is a string or a string?


  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,531
    Yes, stdin (being of type FILE*) is a stream, and buf (being of type char[]) is an array of chars, which can be used to store a string, complete with the \0 at the end.
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,709
    what stored in buf is a string or a string?
    Not much of a choice, is there?

    fgets has the signature:
    Code:
    char *fgets( char *str, int num, FILE *stream )
    So, buf would be a char*, a null terminated string. stdin would be the stream.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

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

    :)

    thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C String Problem: Not reading end of string
    By sedavis4 in forum C Programming
    Replies: 5
    Last Post: 11-17-2008, 09:29 PM
  2. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 12:13 AM
  3. reading and writing to the end of a binary file
    By cloudy in forum C++ Programming
    Replies: 3
    Last Post: 06-06-2005, 04:03 PM
  4. Expression Manipulator v0.2 (bug fixes, functions)
    By ygfperson in forum A Brief History of Cprogramming.com
    Replies: 4
    Last Post: 05-26-2003, 04:52 PM
  5. Reading integers until end of input
    By nivo in forum C Programming
    Replies: 7
    Last Post: 10-20-2001, 04:18 PM

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