Thread: getc

  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
    Chappell Hill, Texas
    Posts
    2,332
    total is never initialized to zero.
    Mainframe assembler programmer by trade. C coder when I can.

  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, 09: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, 11: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, 12:43 PM