1

I am trying to read a file in C language

while(1){
        if(feof(file)){
            break;
        }
        printf("%c",fgetc(file));
    }

At the end I get special character like � I dont have anything like that in file

2
  • 1
    so what's the question here ? Commented Mar 30, 2015 at 15:40
  • 1
    What's the character's decimal representation? Commented Mar 30, 2015 at 15:41

2 Answers 2

5

You can read a file step by step using the following code:

int ch;
while ((ch = fgetc(file)) != EOF) {
    putchar(ch);
}

This special character might be the EOF.

This question/answers might be interesting for you as well.

Sign up to request clarification or add additional context in comments.

Comments

0

From the man page for fgetc:

RETURN VALUE
       fgetc(),  getc()  and getchar() return the character read as an
       unsigned char cast to an int or EOF on end of file or error.

So you should check for feof before you use the return value of fgetc or you will print out the EOF character:

while(1){
        int c = fgetc(file);
        if(feof(file)){
            break;
        }
        printf("%c",c);
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.