getc

This is a discussion on getc within the C Programming forums, part of the General Programming Boards category; Whats wrong with the following: Code: #include <stdio.h> int main() { int c, total; /* declare a char array */ ...

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    270

    getc

    Whats wrong with the following:
    Code:
    #include <stdio.h>
    
    int main() 
    {
    	int c, total;  /* declare a char array */
    	FILE *file;  /* declare a FILE pointer  */
    
    	file = fopen("test.c", "r"); 
    	
    	while((c = getc(file)) != EOF)
    	{
    		total += c;
    	}
    	printf("Total is %d\n", total);
    }
    Given test.c contains:
    Code:
    1 2 45
    The answer i get is not correct.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    What result do you expect?
    I'd guess that you get 49+32+50+32+52+53 -> 268 - or thereabouts as there may be a space, newline or both at the end of the file.

    Did you want to read those numbers as integers, so you get 1 + 2 + 45? If so, you need to use scanf() or some other function that reads the input and converts it to an integer. Right now, you are summing the individual characters with their ascii values, which is of course not the same as adding the numbers together.

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    total is never initialized to zero.
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  4. #4
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by Dino View Post
    total is never initialized to zero.
    Ah, good point, so the result may well be completely rubbish then... ;-)

    -
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  5. #5
    Registered User
    Join Date
    Sep 2008
    Posts
    58
    Code:
    while( fscanf(file, "&#37;d", &n) != EOF) total += n;
    This is another way you could do it.

  6. #6
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    Quote Originally Posted by blurx View Post
    Code:
    while( fscanf(file, "%d", &n) != EOF) total += n;
    This is another way you could do it.
    Yes. However, letting the original poster LEARN by finding the answer him/herself is often a better approach than posting the correction directly - particularly for complete novices (which I assume the original poster is here).

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getc Seg Fault
    By Tyris in forum C Programming
    Replies: 3
    Last Post: 05-25-2005, 12:00 PM
  2. fgets after getc
    By viaxd in forum C Programming
    Replies: 3
    Last Post: 12-03-2003, 08:24 AM
  3. fgetc() and getc()
    By The Dog in forum C Programming
    Replies: 2
    Last Post: 07-24-2002, 05:00 AM
  4. diff btn getc() & getchar()
    By Kokila in forum C Programming
    Replies: 6
    Last Post: 11-29-2001, 10:16 PM
  5. getc or not getc that is the problem
    By Juan Roberto in forum C Programming
    Replies: 3
    Last Post: 11-13-2001, 11:43 AM

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