Please explain this code

This is a discussion on Please explain this code within the C Programming forums, part of the General Programming Boards category; 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 ...

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

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

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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 wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,592
    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.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  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, 09: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, 03: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, 12: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-15-2002, 11:04 PM

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