Chapter-12: File Management in C
Chapter-12: File Management in C
Chapter-12: File Management in C
Chapter-12
File Management in C
Chapter-12:
File Management in C
FILE MANAGEMENT IN C : Q-1 What is File? FILE : File is a place in the disk where a group of related data is stored. File Operations : naming a file opening a file reading data from file writing data to a file closing a file
Q-2 Write the file handling functions (high level I/O functions). fopen() fclose() getc() putc() getw() putw() fprintf() fscanf() fseek() ftell() rewind() creates a new file for use / open a n existing file for use close a file which has been opened for use reads a character from a file writes a character to a file reads an integer from a file writes an integer to a file writes a set of data values to a file reads a set of data values from a file sets the position anywhere in the file gives the current position in the file sets the position to the beginning of the file
Q-3 How to defining and opening a file? To store data in a file in the memory, we must specify : 1. Filename : Filename is a string of characters that make up a valid filename for the operating systems. It may contains two parts, a primary name and an optional period with the extension. Example : hello.c, input.data, test.out 2. Data structure : Data structure of a file is defined as FILE in the library of standard I/O function definitions. FILE is defined data type.
Computer Department/Sardar Patel Institute of Technology, Piludara Page 95
Chapter-12
File Management in C
3. Purpose : Purpose specifies the mode of opening the file General format for declaring and opening a file : FILE *fp; fp = fopen(filename,mode); The first statement declares the variable fp as a pointer to the data type FILE. The second statement opens the file with named filename and assigns an identifier to the fp. In the second statements modes can be one of the followings : r ----- open a file for reading only w ----- create a file for writing only a ----- append (add) to file r+ ---- open a file for read / write w+ --- create a file for read / write a+ ---- append a file for read / write For example : FILE *p1, *p2; p1 = fopen(data, r); p2 = fopen(results, w); Here the file data is opened for reading and results is opened for writing. In case, the data file does not exit , an error will occur. In case, the results file already exits, its contents are deleted and the file is opened as a new file.
Q-4 How to close a file? After completing the operations on the files, the file must be closed using the fclose( ) function. Another case where we have to close a file is when we want to reopen the same file in a different mode. It takes the following form : fclose(file_pointer); Example : fclose(FILE *fp); Here the argument fp is the FILE pointer. All files are closed automatically whenever a program terminates.
Page 96
Chapter-12
File Management in C
Q-5 Explain INPUT/OUTPUT operations on files. The getc and putc functions : The getc is an input function and putc is an output function. These functions can handle only one character at a time. The getc function is used to read a character from a file which is open in read mode. The putc function is used to write a character to a file which is open in write mode. The general forms of getc and putc are : putc(character, fp); getc(fp); Example for putc :
FILE *fp1; fp1 = fopen(DATA, w); putc(d, fp1); Here this statement writes a character contained in the character variable d to the file DATA which is associated with FILE pointer fp1. Example for getc :
FILE *fp2; fp2 = fopen(DATA, r); d = getc(fp2); Here this statement reads a character from the file whose file pointer is fp2. The file pointer moves by one character position for every operation of getc or putc. The getc will return an end of file marker EOF when end of file has been reached. Therefore the reading is terminated when EOF is encountered. The getw and putw functions : The getw is an input function and putw is an output function. These functions are integer oriented functions. These functions can handle only one integer at a time. The getw function is used to read an integer value from a file which is open in read mode. The putw function is used to write an integer to a file which is open in write mode. The general forms of getw and putw are : putw(integer, fp); getw(fp);
Computer Department/Sardar Patel Institute of Technology, Piludara Page 97
Chapter-12
File Management in C
Example for putw : FILE *fp1; fp1 = fopen(DATA, w); putw(d, fp1); Here this statement writes an integer value contained in the integer variable d to the file DATA which is associated with FILE pointer fp1. Example for getw :
FILE *fp2; fp2 = fopen(DATA, r); d = getw(fp2); Here this statement reads an integer value from the file whose file pointer is fp2. The file pointer moves by one integer value position for every operation of getw or putw. The getw will return an end of file marker EOF when end of file has been reached. Therefore the reading is terminated when EOF is encountered. The fprintf and fscanf functions : The fscanf is an input function and fprintf is an output function. These functions can handle group of data at a time. The fprintf function is used to write the data from a file which is open in write mode. The fscanf function is used to input the data to a file which is open in read mode. The general forms of fprintf is : fprintf ( fp, control string, list); Here fp is file pointer associated with a file that has been opened for writing. The control string contains output specifications for the items in the list. The list may include variables, constants and strings. Example : fprintf(f1, %s %d %f, name, age, 7.5); Here, name is a character array variable and age is an int variable. The general forms of fscanf is : fscanf ( fp, control string, list);
Page 98
Chapter-12
File Management in C
Here fp is file pointer associated with a file that has been opened for reading. The control string contains input specifications for the items in the list. The list may include variables, constants and strings. Example : fscanf(f2, %s %d , name, &age); fscanf functions return the EOF when the end of file is reached.
Q-6 Explain ftell and fseek functions on files. ftell( ) : ftell( ) function gives the current position in the file . It takes the following form : n = ftell (fp); Here fp is a FILE pointer associated with the file. The function ftell( ) returns a type long that gives the current file position in bytes from the start of the file (the first byte is at position 0). In case of an error, ftell( ) returns -1L (a type long -1). So n will give the relative offset (in bytes) of the current position. fseek( ) : fseek( ) function is used to set the position anywhere in the file . It takes the following form : fseek( file_pointer, offset, position); Here fp is a FILE pointer associated with the file. The offset specifies the number of positions to be moved from the location specified by position. The offset may be positive, meaning moves forwards, or negative, meaning move backwards. The position can take one of the following values :
VALUE 0 1 2
Chapter-12
File Management in C
MEANING go to the starting stay at the current position go to the end of file move to (m+1)th byte in the file go forwards by m bytes go backward by m bytes from the current position go backward by m bytes from the end
fseek( ) function returns 0 (zero) when the operation is successful. And fseek( ) function returns -1 when an error is occurs. The offset specifies the number of positions to be moved from the location specified by position. *************************************************************
Page 100