Hi-
I'm brand new to the forum and C. I hope this hasn't already been answered. I searched and couldn't find anything, but I'm probably not looking properly. Anyway, I have an assignment in which we are supposed to take a command line argument that is the name of a text file and creates a new text file with a heading line and the contents of the original file w/line numbers added.
If the filename contains a period, use the part of the name before before the period concatenated with .blah as the name of the new file, otherwise just concatenate .blah with the whole filename. Sorry if this is some newbie thing that I should have learned and offends you for asking but I'm lost, I can't get the filename or line numbers coded (truthfully, I haven't tried to hard on the line numbers ).
Hope the code posted correctly. Thank you for your time.Code:/* Goal is to make a backup of the file whose name is the first command line argument. The second command line argument is the name of file. Prepend each new line with the line number and insert a 'title' heading at the top of the new file (I call it backup) that was created. */ #include <stdio.h> int main ( int argc, char *argv[], char ch, char *w, char *x ) { if ( argc != 2 ) // argc should be 2 for correct execution { // We print argv[0] assuming it is the program name printf( "usage: %s <filename> (yes...you have to type a filename)", argv[0] ); } else { // Filename to open FILE *file = fopen( argv[1], "r" ); //First file name to create w = argv[1]; //Create the temporary new file (hopefully to be renamed later) FILE *newfile = fopen ("%s.blah", "w"); //doesn't work, simply creates %s.blah //Add the new Heading to the file fprintf(newfile,"********************%s.blah******************\n\n\n\n", w); /* fopen returns 0, the NULL pointer, on failure */ if ( file == 0 ) { printf( "Could not open file\n" ); } else { /* read one character at a time from file, stopping at EOF, which indicates the end of the file. */ { for (ch = getc(file); ch != EOF; ch = getc(file)) putc(ch, newfile); } } fclose( file ); fclose( newfile ); } }



LinkBack URL
About LinkBacks




, 