reading from text file

This is a discussion on reading from text file within the C Programming forums, part of the General Programming Boards category; Anyone know why this code wouldn't print out what's in the .txt file? Code: #include <stdio.h> #include <stdlib.h> int main( ...

  1. #1
    Registered User jamez's Avatar
    Join Date
    Nov 2005
    Posts
    10

    reading from text file

    Anyone know why this code wouldn't print out what's in the .txt file?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main( void )
    {
         FILE *fp;
    
         if (( fp = fopen( "info.txt", "r" )) == NULL ) 
        {
              printf( "Input file could not be opened\n" );
              exit( 1 );
         }
    
         if ( fclose( fp ) == EOF )
         printf( "File couldn't be closed\n" );
         return 0;
    }
    and the .txt file reads:
    Code:
    printf( "This is the info.txt file.\n" );
    printf( "This is the second line of text from info.txt.\n" );
    printf( "This is the third line of text from info.txt.\n" );
    printf( "This is the fourth line of text from info.txt.\n" );
    printf( "This is the fifth line of text from info.txt.\n" );
    printf( "This is the sixth line of text from info.txt.\n" );
    printf( "This is the seventh lineof text from info.txt.\n" );
    This is the first line without the printf command in info.txt
    This is the second line without the printf command in info.txt
    This is the third line without the printf command in info.txt
    This is the fourth line without the printf command in info.txt
    This is the fifth line without the printf command in info.txt
    This is the sixth line without the printf command in info.txt
    This is the seventh line without the printf command in info.txt

  2. #2
    Bond sunnypalsingh's Avatar
    Join Date
    Oct 2005
    Posts
    162
    because you are not writing any code for printing

  3. #3
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,048
    Nor are you reading anything in from the file. Use getc() to read characters from the file, and print them one by one . . . I'm sure there's something about it in the FAQ or tutorials.
    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.

  4. #4
    cwr
    cwr is offline
    Registered Luser cwr's Avatar
    Join Date
    Jul 2005
    Location
    Sydney, Australia
    Posts
    869
    Though getc will suffice for small files like the above, it's astronomically inefficient. Use fread and read multiple bytes at a time (say 65536).

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, 01:36 PM
  2. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 05:14 AM
  3. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01: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

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