Opening and reading text files

This is a discussion on Opening and reading text files within the C Programming forums, part of the General Programming Boards category; Hello, I am trying to learn C and would like to create a program that opens a text file (already ...

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    8

    Opening and reading text files

    Hello,

    I am trying to learn C and would like to create a program that opens a text file (already created) and reads out the words contained within onto the screen. Not to sure where to stat with this, please can someone give me some ideas?

    Thanks in advance.

  2. #2
    Jack of many languages Dino's Avatar
    Join Date
    Nov 2007
    Location
    Katy, Texas
    Posts
    2,309
    Surely you have a textbook or other book on the basics of C programming?
    Mac and Windows cross platform programmer. Ruby lover.

    Quote of the Day
    12/20: Mario F.:I never was, am not, and never will be, one to shut up in the face of something I think is fundamentally wrong.

    Amen brother!

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    8
    Hi, I have found one of your tutorials and have made substantial progress with my program. Here is the tutorial code

    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 
            {
                int x;
                /* read one character at a time from file, stopping at EOF, which
                   indicates the end of the file.  */
                while  ( ( x = fgetc( file ) ) != EOF )
                {
                    printf( "%c", x );
                }
            }
            fclose( file );
        }
    }
    All i need to do now, is get a loop going so that it can read from a series of text files and not just one. I tried it by adding this:

    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
        int files; 
        int i;  
     
        printf("How many files to open? ");
        scanf("%d",&files);
        for(i = 0; files > 0; i = i + 1);
        {
        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 
            {
                int 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 );
            files = files - 1;
           } 
        }
    }
    But it doesnt work and it just keeps asking "How many files?". Please could someone help.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    32
    Code:
    #include <stdio.h>
    
    int main ( int argc, char *argv[] )
    {
        int files = argc-1, i = 0, x = 0;
        FILE *file;
    
        for(i = 1; i <= files; i++)
        {
                    printf("opening FILE filename = &#37;s\n",argv[i]);
    
                    if ( (file = fopen( argv[i], "r")) == NULL )
                    {
                            fprintf(stderr, "Can't Open file %s\n", argv[i]);
                            continue;
                    }
                    else
                    {
                            while ( (x = fgetc(file)) != EOF )
                            {
                                    printf("%c", x);
                            }
                    }
                    fclose( file );
        }
    return 0;
    }
    Last edited by sillyman; 04-22-2008 at 11:00 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Reading .dat files from a folder in current directory...
    By porsche911nfs in forum C++ Programming
    Replies: 7
    Last Post: 04-04-2009, 09:52 PM
  2. Help with reading strings and opening files PLEASE
    By green2black in forum C Programming
    Replies: 8
    Last Post: 11-17-2008, 04:46 PM
  3. Replies: 5
    Last Post: 02-11-2008, 12:36 AM
  4. help with reading files
    By Unregistered in forum C Programming
    Replies: 1
    Last Post: 04-24-2002, 10:49 PM
  5. Reading Tab Separted Text files
    By Cathy in forum C Programming
    Replies: 1
    Last Post: 02-15-2002, 09:28 AM

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