hopefully a simple oversight

This is a discussion on hopefully a simple oversight within the C Programming forums, part of the General Programming Boards category; Why isn't this working? All I want to do is take a character from one text file and copy it ...

  1. #1
    Registered User
    Join Date
    Aug 2006
    Posts
    13

    hopefully a simple oversight

    Why isn't this working?

    All I want to do is take a character from one text file and copy it in a new text file...

    Code:
    int main(){
    
    #include <stdio.h>
    
    FILE *tf = fopen("./map", "r");
    FILE *af = fopen("./array", "w");
    unsigned char x;
    
    while  ( ( x = fgetc( tf ) ) != EOF )
       {
              fprintf( af, "%c", x);
        }
    
    
    return 0;
    }
    But I get the following warning on the while loop, and of course, an endless loop:



    reader.c: In function `main':
    reader.c:9: warning: comparison is always true due to limited range of data type
    Last edited by dopejack; 10-16-2006 at 08:46 PM.

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    EOF is a negative value and fgetc returns int.
    My best code is written with the delete key.

  3. #3
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    im not familiar with C (or overly familiar with C++ for that matter), but this is how i would change the code: (note i havent tested it but by reading it it seems it would work better)
    Code:
    #include <stdio.h> //these should definetly be before main not in a function!
    
    int main(){
    
    FILE *tf = fopen("./map", "r");
    FILE *af = fopen("./array", "w");
    unsigned char x;
    
    x = fgetc( tf );
    while  ( x != EOF )
    
    return 0;
    }

  4. #4
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    This code works...
    Code:
    #include <stdio.h>
    
    int main(){
    
    FILE *tf = fopen("./map.txt", "r");
    FILE *af = fopen("./array.txt", "w");
    char x;
    
    while  ( ( x = fgetc( tf ) ) != EOF )
            fputc(x,af);
    
    fclose(af);
    fclose(tf);
    
    return 0;
    }
    You said you want to read a single character. If so, why do you use a loop? Why unsigned char? And don't forget to close the file pointers!

    And you cannot store the value you read if you don't write it into the file!

  5. #5
    Registered User
    Join Date
    Aug 2006
    Posts
    13
    Quote Originally Posted by Prelude
    EOF is a negative value and fgetc returns int.

    Exactly right. Changing x to int solved the problem. Interestingly enough, this example was taken from the file i/o tutorial on cprogramming.com....

  6. #6
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >Interestingly enough, this example was taken from the file i/o tutorial on cprogramming.com....
    Barring the forums and a lot of the FAQ, cprogramming.com is a below average resource for programming in my opinion.
    My best code is written with the delete key.

  7. #7
    Registered User
    Join Date
    Oct 2006
    Location
    Canada
    Posts
    1,243
    how come the example code here uses a char and compares that to EOF? a char is simply a int anyways so why shouldnt the above work? i cant try it as i dont have a c compiler.

    edit: nevermind, i guess the solution isnt _exactly_ that it was a char not int, but that it was unsigned not signed. it can still be a char
    Last edited by nadroj; 10-16-2006 at 09:02 PM.

  8. #8
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    The EOF is defined in my stdio.h like below (mingw32)
    Code:
    /* Returned by various functions on end of file condition or error. */
    #define	EOF	(-1)
    it is simply equal to signed int. But the code above includes something like "unsigned char". See the working example I sent.

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >See the working example I sent.
    It only seems to work. char can be either signed or unsigned.
    My best code is written with the delete key.

  10. #10
    Just kidding.... fnoyan's Avatar
    Join Date
    Jun 2003
    Location
    Still in the egg
    Posts
    269
    Quote Originally Posted by Prelude
    It only seems to work. char can be either signed or unsigned.
    Actually, I don't know how they are stored in memory. But, this is the point that makes the compiler to produce "comparison is always true due to limited range of data type" error.

  11. #11
    Registered User
    Join Date
    Mar 2006
    Posts
    725
    The Standard states that char may be implemented in terms of signed or unsigned char. And even then a library would be completely free to define EOF as ((int)-0xdeadbeef), which would be way outside the range of chars on most platforms.

    The compiler nicely warns the programmer on such usage of an unsigned variable, but I doubt it would warn the user who's using a regular implicitly signed char and implicitly casting the return value from fgetc() (gcc doesn't seem to even with -Wall).

    By the way, dopejack, you might want to check if the files were opened correctly before you try to use their handles. [ if(fileptr == NULL || ferror(fileptr)) printf("An error occured\n"); ]
    Code:
    #include <stdio.h>
    
    void J(char*a){int f,i=0,c='1';for(;a[i]!='0';++i)if(i==81){
    puts(a);return;}for(;c<='9';++c){for(f=0;f<9;++f)if(a[i-i%27+i%9
    /3*3+f/3*9+f%3]==c||a[i%9+f*9]==c||a[i-i%9+f]==c)goto e;a[i]=c;J(a);a[i]
    ='0';e:;}}int main(int c,char**v){int t=0;if(c>1){for(;v[1][
    t];++t);if(t==81){J(v[1]);return 0;}}puts("sudoku [0-9]{81}");return 1;}

  12. #12
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. creating very simple text editor using c
    By if13121 in forum C Programming
    Replies: 9
    Last Post: 10-19-2010, 06:26 PM
  2. Simple message encryption
    By Vicious in forum C++ Programming
    Replies: 10
    Last Post: 11-07-2004, 11:48 PM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 04:00 PM
  4. Simple simple program
    By Ryback in forum C++ Programming
    Replies: 10
    Last Post: 09-09-2004, 06:48 AM
  5. Need help with simple DAQ program
    By canada-paul in forum C++ Programming
    Replies: 12
    Last Post: 03-15-2002, 08:52 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21