Presentation is loading. Please wait.

Presentation is loading. Please wait.

Introduction to C Programming CE00312-1 Lecture 15 Files and Program Parameters.

Similar presentations

Presentation on theme: "Introduction to C Programming CE00312-1 Lecture 15 Files and Program Parameters."— Presentation transcript:

1 Introduction to C Programming CE00312-1 Lecture 15 Files and Program Parameters

2 File Input and Output Each file must be explicitly opened before reading or writing (using actual file name as a string) and closed before execution finishes. Input:fscanf reads from a file e.g. in_file = fopen(“data1.dat”, “r”); fscanf(in_file, “%d%d”, &a, &b); Output:fprintf writes to a file e.g. out_file = fopen(“data2.dat”, “w”); fprintf(out_file, “%d %d\n”, a, b);

3 File output example The program on the next slide prompts the user for a file name, reads the file name, prompts for input data, reads 2 numbers from keyboard and writes their sum to the named output file.

4 #include "stdio.h" int main(void) { int a,b,c; char filename[21];// string file name FILE *out_file; // file pointer for output printf("\ntype name of output file: "); // prompt on screen gets(filename); // input from keyboard

5 out_file = fopen(filename, "w"); // open file for output if (out_file == NULL) { printf("\ncannot open: %s", filename); return 1; // abnormal program exit } printf("\ntype 2 integers");// prompt scanf("%d%d", &a, &b);// from keyboard c = a+b; fprintf(out_file, "%d\n", c); // output to file fclose (out_file); return 0;// normal program exit }

6 Program Parameters File names and run-time options can be provided in Unix on the command line when a program is executed. The normal command line a.out could be replaced by, a.out datain dataout where “datain” and “dataout” are the input and output files. These 2 strings must be recognised by the C program so that these files can be used for the input/output. Note that the input file “datain” must already contain data.

7 Program parameters, conventionally called argc and argv, are used to determine which file names and options were supplied. int main(void)// ANSI convention is replaced by: int main(int argc, char* argv[]) // universal where argc is the number of strings and argv is the array of strings

8 For the command line:a.out datain dataout argc is 3, andargv is an array of “a.out”, “datain” and “dataout” Execution options may be similarly specified, conventionally preceded with a ‘-’ so as to be distinguished from file names. For example:a.out datain dataout -option

9 Program Parameter Example Copy all 25 integers from the given input file to the given output file. The input file comprises 25 integers with no formatting but just separated by spaces or new lines. The output file shall comprise 5 rows of 5 integers each separated by a space. There is an option to echo the output on to the screen, where the 2 file names and any “-echo” option are program parameters.

10 #include "stdio.h" #include "string.h" int main(int argc, char *argv[]) // program parameters on command line, // e.g. a.out datain dataout -echo // argc is 4 and argv is // array of these as 4 strings { int num; // for copying each integer int row, col, option = 0; // no echo on screen, by default FILE *myfile_in, *myfile_out; // for 2 files

11 // check for FILE names if (argc < 3) { printf("\nMissing file name(s).\n"); printf("Too few parameters %d", argc); return 1; // abnormal exit from program } // the 2 file names cannot be the same if ((strcmp(argv[1], argv[2]) == 0)) { printf("\nsame file names !\n"); return 1; // abnormal exit }

12 // open first file for input myfile_in = fopen(argv[1], "r"); if (myfile_in == NULL) // check if input file exists { printf("\ncan’t open input file:%s\n", argv[1]); return 1;// abnormal exit } // open second file for output myfile_out = fopen(argv[2], "w"); if (myfile_out == NULL)// playing safe! { printf("\ncan’t open O/P file:%s\n", argv[2]); fclose(myfile_in);// already opened return 1;// abnormal exit }

13 // check option, should be -echo if (argc == 4) // 4th parameter { if (strcmp(argv[3],"-echo") == 0) { option = 1;// echo on screen } else { printf("illegal %s\n", argv[3]); printf("must be -echo\n"); fclose(myfile_in);// already fclose(myfile_out);// opened return 1;// abnormal exit } // else no 4th parameter specified, // option remains 0

14 for (row=0; row<5; row++) {// copy each row for (col=0; col<5; col++) {// copy each column fscanf(myfile_in, “%d”, &num); fprintf(myfile_out, “%d ”, num); // after each integer is a space if (option)// option == 1 printf(“%d ”, num);// echo } fprintf(myfile_out, “\n”); // end row if (option) printf(“\n”);// echo } fclose(myfile_in); fclose(myfile_out); return 0;// normal exit }// end main

Download ppt "Introduction to C Programming CE00312-1 Lecture 15 Files and Program Parameters."

Similar presentations

Ads by Google