Hi, please reference this with my code below.
I have created 3 text files (name.txt, name2.txt and name3.txt) Each of the 3 text files contains 3 words, so 9 words are displayed in total at the end of my program (this is why argc !=4). 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 per line.
Thanks very much for any assistance anyone is able to give me with this problem.
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 ); } } }



LinkBack URL
About LinkBacks


