Thread: Please explain this code

  1. #1
    Registered User
    Join Date
    Aug 2009
    Posts
    27

    Please explain this code

    Code:
        while(fgets(buffer,127,pInfile)!=NULL)
        {
            for(i = 0; buffer[i]; ++i)
            {
                buffer[i] = tolower(buffer[i]);
                printf("%c",buffer[i]);
                count[buffer[i] - 'a']++;
            }
        }
    When I read text from a file,
    i don't understand about the second argument of the for loop,
    what does it mean for the for loop to run until buffer[i],
    i don't understand the meaning of buffer[i] in the for loop argument
    because usually it is sth like for(i=0; i<=5; i++)

    Please explain me T___T;

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    It's the same as

    buffer[i] != '\0'

    In other words, all the chars up to 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.

  3. #3
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    the end doesn't include space bar right?

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    Try it!
    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
    Aug 2009
    Posts
    27
    Thanks for helping !
    This clear my confusion

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    May i ask a bit more,
    what is the difference between these two
    >while(fgets(buffer,127,pInfile)!=NULL)
    >while ( fscanf( pInfile, "%s" , buffer ) != EOF )
    or are they the same?

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    No they are not the same.

    Assuming 127 is an honest statement of the size of the input buffer, fgets() will never overflow the buffer.
    It will also read spaces.

    The fscanf will not read spaces (as written), nor can it guarantee to never overflow the buffer.
    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.

  8. #8
    Registered User
    Join Date
    Aug 2009
    Posts
    27
    so if i also want to count the frequency of the space then i should use the fgets one
    because the fscanf doesn't read space, right?

  9. #9
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It can, provided you use the correct format specifier. You could just do it the lazy way and use fgetc. You're going to be reading through each character at a time anyway.


    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Can someone explain a line of code for me please?
    By drkidd22 in forum C Programming
    Replies: 8
    Last Post: 12-09-2009, 10:36 PM
  2. True ASM vs. Fake ASM ????
    By DavidP in forum A Brief History of Cprogramming.com
    Replies: 7
    Last Post: 04-02-2003, 04:28 AM
  3. Seems like correct code, but results are not right...
    By OmniMirror in forum C Programming
    Replies: 4
    Last Post: 02-13-2003, 01:33 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM
  5. Replies: 4
    Last Post: 01-16-2002, 12:04 AM