Thread: reading from text file

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    reading from text file

    i know this has to be simple, but ive been reading the tutorials, and searched through about 15 pages of threads looking for my problem to no avail .

    the .txt file is nothing more than "123456789". i was hoping to read one character at a time and eventually put each in an array[10], but i thought i would just try to print it out on screen first. i don't get a console window at all.


    Code:
    #include <stdio.h>
    
    int main()
    {
    
       FILE *otn = fopen ("c:\\onethroughnine.txt", "r");
       unsigned char x;
       while  ( ( x = fgetc( otn ) ) != EOF ) {
              printf( "%c", x );
       }
       fclose ( otn );
       getchar();
    }


    the compiler reports no errors, but this output:

    p:\c projects\untitled1.c: In function `main':
    p:\c projects\untitled1.c:8: warning: comparison is always true due to limited range of data type

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
    unsigned char x;
    ->
    Code:
    int x;
    EOF is an int value.
    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.

  3. #3
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    change unsigned char x, to just char x.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  4. #4
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    No, char x would be worse. int x is what you want. See the FAQ.
    Last edited by dwks; 11-04-2005 at 04:37 PM.
    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.

  5. #5
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    perfect!


    maybe you could help me understand from the tutorial example why unsigned char is used:

    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
        if ( argc != 2 ) /* argc should be 2 for correct execution */
        {
            /* We print argv[0] assuming it is the program name */
            printf( "usage: %s filename", argv[0] );
        }
        else 
        {
            // We assume argv[1] is a filename to open
            FILE *file = fopen( argv[1], "r" );
    
            /* fopen returns 0, the NULL pointer, on failure */
            if ( file == 0 )
            {
                printf( "Could not open file\n" );
            }
            else 
            {
                unsigned char x;
                /* read one character at a time from file, stopping at EOF, which
                   indicates the end of the file.  Note that the idiom of "assign
                   to a variable, check the value" used below works because
                   the assignment statement evaluates to the value assigned. */
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "%c", x );
                }
            }
            fclose( file );
        }
    }

    thanks for the help

  6. #6
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    That is definitely wrong. See the FAQ link I provided.
    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.

  7. #7
    Bioport Productions
    Join Date
    Oct 2005
    Posts
    215
    They're right, one thing you could do is typecast fgetc to return a char instead of an int. EOF is -1 so you will need a signed char to do it.
    -"What we wish, we readily believe, and what we ourselves think, we imagine others think also."
    PHP Code:
    sadf 

  8. #8
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    thanks alot for your help. up and running.

  9. #9
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    They're right, one thing you could do is typecast fgetc to return a char instead of an int. EOF is -1 so you will need a signed char to do it.
    Did you read the FAQ?

    Yes, you could typecast it, but what if the file contains the character (-1, I mean)?
    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. Formatting the contents of a text file
    By dagorsul in forum C++ Programming
    Replies: 2
    Last Post: 04-29-2008, 12:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  4. Reading Character at a time from a text file
    By Giania in forum C Programming
    Replies: 8
    Last Post: 02-25-2006, 03:17 PM
  5. A bunch of Linker Errors...
    By Junior89 in forum Windows Programming
    Replies: 4
    Last Post: 01-06-2006, 02:59 PM