Chapter-12
File Management in C
Copyright © 2016 McGraw Hill Education, All Rights Reserved.
PROPRIETARY MATERIAL © 2016 The McGraw Hill Education, Inc. All rights reserved. No part of this PowerPoint slide may be displayed, reproduced
or distributed in any form or by any means, without the prior written permission of the publisher, or used beyond the limited distribution to teachers and
educators permitted by McGraw Hill for their individual course preparation. If you are a student using this PowerPoint slide, you are using it without
permission.
Introduction
• A file is a place on the disk where a group of related data is stored.
• Important high-level I/O functions available in C Library are:-
Function name Operation
fopen() Creates a new file for use.
Opens an existing file for use.
fclose() Close a file which has been opened for use
getc() Reads a character from a file
putc() Writes a character to a file
fprintf() Writes a set of data values to a file
fscanf() Reads a set of data values from a file
getw() Reads an integer from a file
putw() Writes an integer to a file
fseek() Sets the position to a desired point in a file
ftell() Gives the current position in the file (in bytes from beginning)
rewind() Sets the position to the beginning of the file
Defining and Opening a File
• The following must be specified to the OS about the file:-
• Filename- String of characters. E.g. input.data, PROG.C, Text.out
• Data Structure- of a file is defined as FILE in standard I/O library.
• Purpose- What to do with the file.
• General format for declaring and opening a file:-
FILE *fp;
fp = fopen(“filename”, “mode”);
• FILE *fp; declares fp as pointer to FILE data type.
• mode can be one of the following:-
• r open the file for reading only. (file not exists - error)
• w open the file for writing only. (file not exists – create new
file, file exist – delete contents)
• a open the file for appending (or adding) data to it.
(Current contents are not deleted)
• FILE *p1, *p2;
• p1 = fopen(“data”, “r”);
• p2 = fopen(“results”, “w”);
• The file data is opened for reading and results is opened for writing.
• If results file already exists, its content is deleted and the file is
opened as a new file.
• If data does not exists, an error will occur.
• Multiple files can be opened and use at a time.
Closing a File
• The general form is:
• fclose(file_pointer); //close file associated with the FILE
pointer file_pointer
• Example:
----
FILE *p1, *p2;
p1 = fopen(“INPUT”, “w”);
p2 = fopen(“OUTPUT”, “r”);
----
----
fclose(p1);
fclose(p2);
Input/Output
• getc and putc:- Analogous to getchar and putchar functions and
handle one character at a time.
• e.g- putc(c, f1); //Writes character in variable c to file
associated with FILE pointer fp1.
• getc is used to read one character from a file that is opened in read
mode.
• e.g.- c = getc(fp2); //read character from file whose file pointer is fp2
• The file pointer moves by one character position for every operation
of getc and putc.
• Reading terminated when EOF is encountered.
Fig. 12.1 Character oriented read/write operations on a file
• getw and putw functions:-
• Integer-oriented functions.
• Similar to getc and putc functions and are used to read and write
integer values.
• General form are:-
• putw(integer, fp);
• getw(fp);
(Contd.)
(Contd.)
Contents of ODD file
111 333 555 777 999 121 343 565
Contents of EVEN file
222 444 666 888 0 232 454
Fig. 12.2 Operations on integer data
• The fprintf and fscanf functions:-
• They can handle group of mixed data simultaneously.
• General form of fprintf is:-
• fprintf(fp, “control string”, list);
• fp is a file pointer associated with a file opened for writing.
• control strings- contains output specifications for the items in
the list. The list may include variables, constants and strings.
• e.g- fprintf(f1, “%s %d %f”, name, age, 7.5);
• General format of fscanf is:-
• fscanf(fp, “control string”, list);
• read items in the list from the file specified by fp, according to
the specifications contained in the control string.
• E.g- fscanf(f2, “%s %d”, item, &quantity);
(Contd.)
Fig. 12.3 Operations on mixed data types
Command Line Arguments
• A Command Line Argument is a parameter supplied to a program
when the program is invoked.
• This parameter may represent a filename the program should
process.
• e.g- C > PROGRAM X_FILE Y_FILE
• //Where PROGRAM is filename where executable program is
stored.
• Main function can take two arguments argc and argv.
• The information contained in the command line is passed on to the program
through these arguments, when main is called by the system.
• For above e.g. argc is 3, and argv is array of three pointerss to
string:
• argv[0] -> PROGRAM
• argv[1] -> X_FILE
• argv[2] -> Y_FILE
• In order to access the command line arguments, the main function
and its parameters are declared as follows:
main (int argc, char *argv[ ]){
…….
…….
}
(Contd.)
Fig. 12.7 Use of command line arguments