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.

Hi thanks for the advice. I have now found a tutorial and have made substantial progress with my program. Here is my code so far

#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:

#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.

In order to access a file a few key components need to play together.
A string with the name/location of the file.
A FILE pointer that will become the handle for that file.
A function named fopen() to try to open that file for reading, writing, appending, etc...
A function named fclose() when you are done using that file.

You have been shown how to obtain the string file name from the command line, i.e. argv[1]. More files could be argv[2], argv[3],...

However, a file string can be held after the command line. char filename[] = "Myfile.txt"; or char *filename = "Myotherfile.txt"; You can even specify the path for it: char *filename = "C:\\Myhome\\Myfile.txt"; or char filename[] = "/home/I/myfile.txt"; Each of these strings need to be associated with a FILE pointer and opened through it, by using fopen().
i.e.

FILE *fileHandle;

fileHandle = fopen( filename, "r" );

It is always imperative to check if the access was successful

if ( fileHandle  == NULL ) {
 /* error occur */
}

Each file must be closed after the work is done with it. fclose( fileHandle ); No need to check if the closing was successful.

You can declare an array of FILE pointers for each distinctive file names

FILE *fileHandle[MAX];

fileHandle[0] = fopen( "Myfile", "r" );
.
.
.
fileHandle[1] = fopen( "MyOtherFile", "r" );

or you can do work on one and when you are finished use the same FILE pointer to open the next file.

Ok thank you. I have now got the loop working. I created 3 text files (name.txt, name2.txt and name3.txt) hence why arc now "!= 4". Each of the 3 text files contains 3 words, so 9 words are displayed in total at the end of my program. The only thing i need to do now is get it to take all the 9 words and write them to an output file, one word at a time.

Thanks very much for your assistance with this, any help you can give me on this last step would be very much appreciated as well.

FYI, here is my updated code:

#include <stdio.h>

int main ( int argc, char *argv[] )
{
  int i;
  if ( argc != 4 ) /* 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
        for(i = 1; i < 4; i = i+1) // for loop (starting; while_true; do_this).
          {
            FILE *file = fopen( argv[i], "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 );
              }
          }
            printf(" \n"); //ensures first word from next text file is on a new line. 
            fclose( file );
          }

      }
}
This article has been dead for over six months. Start a new discussion instead.