SOFTWARE DEVELOPMENT IN C CM9041 - PowerPoint PPT Presentation

1 / 15
About This Presentation
Title:

SOFTWARE DEVELOPMENT IN C CM9041

Description:

where argc is the number of strings and argv is the array of strings. Thus, for ... and argv is an array of 'a.out', 'datain' and 'dataout' ... – PowerPoint PPT presentation

Number of Views:40
Avg rating:3.0/5.0
Slides: 16
Provided by: rho2
Category:

less

Transcript and Presenter's Notes

Title: SOFTWARE DEVELOPMENT IN C CM9041


1
SOFTWARE DEVELOPMENT IN CCM904-1
  • Lecture 9
  • Input/Output with Files

2
  • Each file must be explicitly opened before
    reading or writing (using actual file name as a
    string) and closed before execution finishes.
  • Creates an internal file buffer linked to an
    external physical location or file address

3
Input
  • fscanf reads from a file
  • in_file fopen(data1.dat, r)
  • fscanf(in_file, dd, a, b)

4
Output
  • fprintf writes to a file
  • out_file fopen(data2.dat, w)
  • fprintf(out_file, d d\n, a, b)

5
Example
  • This program prompts the user for a file name,
  • reads file name 2 numbers from keyboard
  • and writes the sum to the named file.

6
  • include "stdio.h"
  • int main(void)
  • int a,b,c
  • char filename21
  • // max of 20 characters for file name
  • FILE out_file
  • // pointer to a buffer for output file
  • printf("\ntype name of output file ")
  • // prompt on screen
  • gets(filename) // input from keyboard
  • out_file fopen(filename, "w")
  • // open file for output
  • if (out_file NULL)
  • printf("\ncannot open file s",filename)
  • return 1
  • // abnormal exit from program

7
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, for example
  • a.out datain dataout

8
  • a.out datain dataout
  • could be specified to mean the input file is
    called datain and the output file is called
    dataout. These 2 strings must be recognised by
    the C program so that these files can be used for
    the input/output. Note that file datain must
    already contain data.

9
  • 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

10
  • Thus, for
  • a.out datain dataout
  • argc is 3,
  • and argv is an array of
  • a.out, datain and dataout
  • Execution options may be similarly specified,
    conventially preceded with a - to be
    distinguished from file names.
  • For example
  • a.out datain dataout -option

11
Example
  • Copy all 25 integers from the given input file to
    the given output file. The input file comprises
    25 integers 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.

12
  • 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

13
  • //check for FILE names
  • if (argc lt 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(argv1, argv2) 0))
  • printf("\nsame file names !\n")
  • return (1) // abnormal exit
  • // open first file for input
  • myfile_in fopen(argv1, "r")
  • if (myfile_in NULL)
  • // check if input file exists

14
  • // open second file for output
  • myfile_out fopen(argv2,"w")
  • if (myfile_out NULL) // playing safe!
  • printf("\ncant open output files\n",argv2)
  • fclose(myfile_in) // already opened
  • return (1) // abnormal exit
  • // check option, should be -echo
  • if (argc 4) // 4th parameter specified
  • if ( strcmp(argv3, "-echo") 0)
  • option 1 // echo on screen
  • else
  • printf("illegal s\n", argv3)
  • printf("must be -echo\n")

15
  • // else no 4th parameter specified,
  • // option remains 0
  • // Copy from myfile_in to myfile_out
  • // and with echo on the screen option
  • for (row0 rowlt5 row)
  • for (col0 collt5 col)
  • 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)
Write a Comment
User Comments (0)
About PowerShow.com