Thread: Unknown Error(c-lang), while reading a character from file

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    3

    Question Unknown Error(c-lang), while reading a character from file

    I wrote a code in C to read a character from external .txt file. The codes are given below.

    #include<stdio.h>

    void main()
    {
    FILE *in;
    unsigned char ch;
    in=fopen("file.txt","r");
    while((ch=fgetc(in))!=EOF)
    {
    printf("%c",ch);
    }
    }

    after campile it gave the warning :
    (ch=fgetc(in))!=EOF) is always true and would be in infinite loop.

    But when I removed the unsigned declaration of ch it works fine.

    Please, tell me , Why did it give me Warning like that ?

  2. #2
    Registered User Scribbler's Avatar
    Join Date
    Sep 2004
    Location
    Aurora CO
    Posts
    266
    EOF typically is the integer value -1.

    Since you defined unsigned char ch; the variable ch is an unsigned value, which will never be able to be -1.

    Also...try remember to post your code inside of [code] tags. And also try to keep questions pertaining to C in the C forums (this is the C++ forum).
    Last edited by Scribbler; 01-14-2005 at 03:29 AM.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,268
    Also remember to use int main() instead of void main().

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Newbie homework help
    By fossage in forum C Programming
    Replies: 3
    Last Post: 04-30-2009, 04:27 PM
  2. Replies: 7
    Last Post: 02-02-2009, 07:27 AM
  3. Replies: 9
    Last Post: 12-08-2008, 10:27 AM
  4. Formatting a text file...
    By dagorsul in forum C Programming
    Replies: 12
    Last Post: 05-02-2008, 03:53 AM
  5. problem in reading alphabets from file
    By gemini_shooter in forum C Programming
    Replies: 6
    Last Post: 03-09-2005, 01:49 PM