Download presentation
Presentation is loading. Please wait.
Published byMonica King Modified over 9 years ago
1
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur1 File Handling in C Lecture 17c 20/3/01
2
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur2 Introduction zFiles are places where data can be stored permanently. zSome programs expect the same set of data to be fed as input every time it is run. yCumbersome. yBetter if the data are kept in a file, and the program reads from the file. zPrograms generating large volumes of output. yDifficult to view on the screen. yBetter to store them in a file for later viewing/ processing
3
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur3 Basic File Operations zOpening a file zReading data from a file zWriting data to a file zClosing a file
4
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur4 Opening a File zA file must be “opened” before it can be used. FILE *fp; : fp = fopen (filename, mode); yfp is declared as a pointer to the data type FILE. yfilename is a string - specifies the name of the file. yfopen returns a pointer to the file which is used in all subsequent file operations. ymode is a string which specifies the purpose of opening the file: “r” :: open the file for reading only “w” :: open the file for writing only “a” :: open the file for appending data to it
5
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur5 Contd. zPoints to note: ySeveral files may be opened at the same time. yFor the “w” and “a” modes, if the named file does not exist, it is automatically created. yFor the “w” mode, if the named file exists, its contents will be overwritten.
6
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur6 Examples FILE *in, *out ; in = fopen (“mydata.dat”, “r”) ; out = fopen (“result.dat”, “w”); FILE *empl ; char filename[25]; scanf (“%s”, filename); empl = fopen (filename, “r”) ;
7
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur7 Closing a File zAfter all operations on a file have been completed, it must be closed. yEnsures that all file data stored in memory buffers are properly written to the file. zGeneral format: fclose (file_pointer) ; FILE *xyz ; xyz = fopen (“test”, “w”) ; ……. fclose (xyz) ;
8
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur8 Read/Write Operations on Files zThe simplest file input-output (I/O) function are getc and putc. zgetc is used to read a character from a file and return it. char ch; FILE *fp; ….. ch = getc (fp) ; ygetc will return an end-of-file marker EOF, when the end of the file has been reached. zputc is used to write a character to a file. char ch; FILE *fp; …… putc (c, fp) ;
9
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur9 Example :: convert a text file to all UPPERCASE main() { FILE *in, *out ; char c ; in = fopen (“infile.dat”, “r”) ; out = fopen (“outfile.dat”, “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ; }
10
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur10 Contd. zWe can also use the file versions of scanf and printf, called fscanf and fprintf. zGeneral format: fscanf (file_pointer, control_string, list) ; fprintf (file_pointer, control_string, list) ; zExamples: fscanf (fp, “%d %s %f”, &roll, dept_code, &cgpa) ; fprintf (out, “\nThe result is: %d”, xyz) ;
11
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur11 Some Points zHow to check EOF condition when using fscanf? yUse the function feof if (feof (fp)) printf (“\n Reached end of file”) ; zHow to check successful open? yFor opening in “r” mode, the file must exist. if (fp == NULL) printf (“\n Unable to open file”) ;
12
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur12 Example typedef struct { int roll; char dept_code[6]; float cgpa; } STUD; main() { FILE *stud; STUD s; float sum = 0.0; int count = 0; stud = fopen (“stud.dat”, “r”) ; while (1) { if (feof (stud)) break; fscanf (stud, “%d %s %f”, &s.roll, s.dept_code, &s.cgpa); count ++; sum += s.cgpa; } printf (“\nThe average cgpa is %f”, sum/count); fclose (stud); }
13
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur13 Arguments to main () zCommand line arguments are parameters supplied to a program, when the program is invoked. cc myfile.c cc xyz.c -lm netscape www.mailcity.com average 10 20 30 40 50 zHow do these parameters get into the program? yEvery C program has a main function. ymain can take two arguments conventionally called argc and argv. yInformation regarding command line arguments are passed to the program through argc and argv.
14
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur14 Echoing the command line arguments int main (int argc, char *argv[]) { int i; printf (“argc = %d\n”, argc) ; for (i=0; i<argc; ++i) printf (“argv[%d] = %s\n”, i,argv[i]) ; return 0; } $ a.out how many argc = 3 argv[0] = a.out argv[1] = how argv[2] = many
15
20/03/01 Sudeshna Sarkar, CSE, IIT Kharagpur15 Example :: convert a text file to all UPPERCASE, using command line arguments main (int argc, char *argv[ ] { FILE *in, *out ; char c ; in = fopen (argv[1], “r”) ; out = fopen (argv[2], “w”) ; while ((c = getc (in)) != EOF) putc (toupper (c), out); fclose (in) ; fclose (out) ; } Run this program as: a.out old new
Similar presentations
© 2024 SlidePlayer.com. Inc.
All rights reserved.