Thread: Strange question mark at EOF

  1. #1
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352

    Strange question mark at EOF

    I made a simple program that reads a file and prints the contents of the file to the output.

    Code:
    #include <stdio.h>
    
    int main(int argc, char *argv[])
    {
    	if (!argv[1])
    	{
    		printf("USAGE: %s [filename]\n", argv[0]);
    		return 1;
    	}
    	
    	FILE *in = (fopen(argv[1], "r"));
    	char *file;
    	while (file != EOF)
    	{
    		file = fgetc(in);
    		printf("%c", file);
    	}
    	printf("\n");
    	return 0;
    }
    It prints the file contents to the output like it should, but there's a strange question mark at the end of it. I typed, ./prog foobar into the Terminal and it printed the following.
    Code:
    Hello World!
    ?
    There is no question mark at the end of the foobar file. Is it printing the terminating null byte? Can someone explain this to me?

  2. #2
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by Babkockdood View Post
    char *file;
    while (file != EOF)
    BAD!!!

    You cannot use a "char" because each byte in the file fills a whole "char". How do you think the EOF can be represented then? You have to use an int instead. It's filled with the values 0-255 for a byte from the file, and a -1 for EOF.

    And why is the char a pointer?

    Also, you check if the file reached EOF before getting the next byte and printing it. You have to check between the reading of the byte and the printing. In your current code, the char isn't EOF, you read it, it's and EOF, but you brint it before checking!

    Here's the correct code:

    Code:
    int file;
    while (file = fgetc(in))
    {
            putchar(file);
    }
    Last edited by MTK; 06-08-2010 at 07:19 PM.

  3. #3
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by MTK View Post
    BAD!!!

    You cannot use a "char" because each byte in the file fills a whole "char". How do you think the EOF can be represented then? You have to use an int instead. It's filled with the values 0-255 for a byte from the file, and a -1 for EOF.

    Also, you check if the file reached EOF before getting the next byte and printing it. You have to check between the reading of the byte and the printing.
    OK, I made file an int and removed the asterisk. I'm not getting the same warning I was earlier, but it's still printing the question mark.

  4. #4
    Third Eye Babkockdood's Avatar
    Join Date
    Apr 2010
    Posts
    352
    Quote Originally Posted by MTK View Post
    Here's the correct code:

    Code:
    int file;
    while (file = fgetc(in))
    {
            putchar(file);
    }
    I saw your edited post, and replaced the old code with the above. It was an endless loop that printed only question marks. I got it working. This is what I used.
    Code:
    int file;
    	while ((file = fgetc(in)) != EOF)
    	{
    		putchar(file);
    	}
    Thanks for pointing that out, MTK.

  5. #5
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    I wonder what compiler you are using.
    and how could you ignore all your compiler warnings (if there is any?)

  6. #6
    Registered User
    Join Date
    Aug 2009
    Posts
    198
    Quote Originally Posted by Babkockdood View Post
    Code:
    int file;
    	while ((file = fgetc(in)) != EOF)
    	{
    		putchar(file);
    	}
    Yeah, that's correct. Without the "!= EOF" part it checks if it's not 0 instead of not EOF (probably -1).

    And as you see the assignment operator can be used within an expression! It assigns the value to the right to the variable on the left, and evaluates to the new value of the variable.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about EOF.
    By Junky in forum C Programming
    Replies: 20
    Last Post: 03-31-2011, 01:43 PM
  2. while ((c = getchar()) != EOF), AND cntrl z
    By Roger in forum C Programming
    Replies: 8
    Last Post: 10-21-2009, 09:25 PM
  3. Exam Question - Possible Mistake?
    By Richie T in forum C++ Programming
    Replies: 15
    Last Post: 05-08-2006, 03:44 PM
  4. EOF messing up my input stream?
    By Decrypt in forum C++ Programming
    Replies: 4
    Last Post: 09-30-2005, 03:00 PM
  5. question about eof
    By ssjnamek in forum C++ Programming
    Replies: 17
    Last Post: 05-05-2005, 10:05 AM