Thread: smiley faces ????

  1. #1
    Registered User
    Join Date
    Jan 2008
    Posts
    124

    smiley faces ????

    Hello,
    I am trying to make my program read a file thats on my desktop.
    When I run the code all I get is smiley faces.
    What is the problem with my code?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <stddef.h>
    
    int main (){
    	char c;
    	char inputText[500];
    
    	FILE* inputFile;
    
    	inputFile=fopen("C:\\Documents and Settings\\User\\My Documents\\GPA.txt","r");
    
    	if (inputFile==NULL){
    		printf("Failed to open");
    		exit(-1);
    	}
    
    	fgets(inputText, 500, inputFile);
    
    	while ((c=getc(inputFile)!=EOF)){
    	printf("%c",c);
    	}
    	fclose(inputFile);
    	system("Pause");
    }

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    You could be trying to read in int's, not char's. Try changing c to an int, and print it with %d, instead of %c.

    If that doesn't work, paste up a bit of the input file so we can see what's going on (in code tabs, of course).

  3. #3
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    First off, Adak is right in that c needs to be an int, not a char (even though you're reading characters!). Note however that you needn't use &#37;d; if you're printing out characters, %c is correct, even though c is an int.

    Next, take a look at this:
    Code:
    while ((c=getc(inputFile)!=EOF)){
    Notice how you surround the whole expression with two sets of parentheses? One set is redundant. The result is code that always assigns either 0 or 1 to c; hence the smiley faces. You meant to do:
    Code:
    while( (c = getc(inputFile)) != EOF )
    Otherwise, it's parsed as:
    Code:
    while(c = (getc(inputFile) != EOF))
    So you see how the result of the != operator (which yields 0 or 1) is stored into c.
    Last edited by cas; 04-16-2008 at 09:37 PM. Reason: Hopefully clarify char vs. int a little bit.

  4. #4
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Code:
    fgets(inputText, 500, inputFile);
    Is there a specific reason why you're skipping the first 500 bytes of the file? If that really is your intention, fseek( inputFile, 500, SEEK_SET ); might be more appropriate.

  5. #5
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    good catch, cas!

  6. #6
    Cogito Ergo Sum
    Join Date
    Mar 2007
    Location
    Sydney, Australia
    Posts
    463
    I just found it funny; this thread, I don't know why. I can just imagine someone's face as they run the program and they have ASCII faces staring at them. 'What on earth?'
    =========================================
    Everytime you segfault, you murder some part of the world

  7. #7
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by cpjust View Post
    Code:
    fgets(inputText, 500, inputFile);
    Is there a specific reason why you're skipping the first 500 bytes of the file? If that really is your intention, fseek( inputFile, 500, SEEK_SET ); might be more appropriate.
    And more impossible, if the inputFile happens to be a pipe.

  8. #8
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    and of course it will result in different output if the first line is shorter than 500 chars
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. 3D rendering of only visible faces
    By Zeeshan in forum Game Programming
    Replies: 5
    Last Post: 02-23-2009, 06:52 PM
  2. OpenGL cube's faces mess up
    By ay_okay in forum Game Programming
    Replies: 5
    Last Post: 06-08-2005, 07:05 PM
  3. Sending problem, or 1 = smiley face
    By Asmodeus in forum Tech Board
    Replies: 1
    Last Post: 04-25-2005, 11:01 AM
  4. Smiley Faces?
    By 0rion in forum C Programming
    Replies: 15
    Last Post: 06-08-2004, 07:29 AM
  5. Your Favorite Smiley...
    By doubleanti in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-22-2001, 10:49 PM