Computer Programming & Utilization

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

 In case.  The second statement opens the file with named filename and assigns an identifier to the fp.open a file for read / write w+ --. p2 = fopen(“results”.  Another case where we have to close a file is when we want to reopen the same file in a different mode. Q-4 How to close a file?  After completing the operations on the files.  Here the argument fp is the FILE pointer.  All files are closed automatically whenever a program terminates.  In case. fp = fopen(“filename”.  It takes the following form : fclose(file_pointer). an error will occur.open a file for reading only w ----.  The first statement declares the variable fp as a “pointer to the data type FILE”. *p2. “r”). p1 = fopen(“data”.  Example : fclose(FILE *fp). Computer Department/Sardar Patel Institute of Technology.create a file for writing only a ----.  In the second statements modes can be one of the followings : r ----. Piludara Page 96 . the file must be closed using the fclose( ) function. the data file does not exit .”mode”). Purpose : Purpose specifies the mode of opening the file  General format for declaring and opening a file : FILE *fp. “w”).append a file for read / write  For example : FILE *p1.create a file for read / write a+ ---.  Here the file data is opened for reading and results is opened for writing. the results file already exits.Computer Programming & Utilization Chapter-12 File Management in C 3. its contents are deleted and the file is opened as a new file.append (add) to file r+ ---.

 The getw and putw functions :     The getw is an input function and putw is an output function. fp2 = fopen(“DATA”.  Here this statement writes a character contained in the character variable d to the file DATA which is associated with FILE pointer fp1. Computer Department/Sardar Patel Institute of Technology. fp1).  Here this statement reads a character from the file whose file pointer is fp2. fp1 = fopen(“DATA”. The getw function is used to read an integer value from a file which is open in read mode.Computer Programming & Utilization Chapter-12 File Management in C Q-5 Explain INPUT/OUTPUT operations on files.  The putw function is used to write an integer to a file which is open in write mode.  The file pointer moves by one character position for every operation of getc or putc.  The general forms of getw and putw are : putw(integer. Piludara Page 97 . fp). These functions can handle only one integer at a time. The general forms of getc and putc are : putc(character. These functions can handle only one character at a time. getc(fp). getw(fp).  The getc will return an end of file marker EOF when end of file has been reached.  The getc and putc functions :      The getc is an input function and putc is an output function.  Example for getc : FILE *fp2. The getc function is used to read a character from a file which is open in read mode. “w”). These functions are integer oriented functions. d = getc(fp2). “r”). putc(d. fp). The putc function is used to write a character to a file which is open in write mode. Therefore the reading is terminated when EOF is encountered.  Example for putc : FILE *fp1.

The fscanf function is used to input the data to a file which is open in read mode. putw(d. The general forms of fprintf is : fprintf ( fp. name is a character array variable and age is an int variable. The list may include variables. list). constants and strings.  Here this statement reads an integer value from the file whose file pointer is fp2.  The general forms of fscanf is : fscanf ( fp. “control string”. The fprintf function is used to write the data from a file which is open in write mode.Computer Programming & Utilization Chapter-12 File Management in C  Example for putw : FILE *fp1. fp1). “r”).  The getw will return an end of file marker EOF when end of file has been reached. d = getw(fp2).  Here fp is file pointer associated with a file that has been opened for writing. Computer Department/Sardar Patel Institute of Technology.  Here.  Here this statement writes an integer value contained in the integer variable d to the file DATA which is associated with FILE pointer fp1.  The file pointer moves by one integer value position for every operation of getw or putw. Therefore the reading is terminated when EOF is encountered. fp2 = fopen(“DATA”. Piludara Page 98 . age. “w”).  Example : fprintf(f1. These functions can handle group of data at a time. “%s %d %f”. name.  The fprintf and fscanf functions :      The fscanf is an input function and fprintf is an output function.  Example for getw : FILE *fp2. “control string”. 7. list). fp1 = fopen(“DATA”. The control string contains output specifications for the items in the list.5).

 It takes the following form : fseek( file_pointer. or negative.Computer Programming & Utilization Chapter-12 File Management in C  Here fp is file pointer associated with a file that has been opened for reading. Q-6 Explain ftell and fseek functions on files.  The offset specifies the number of positions to be moved from the location specified by position. Piludara . The list may include variables. meaning move backwards.  The position can take one of the following values : VALUE 0 1 2 MEANING Starting of file Current position End of file Page 99 Computer Department/Sardar Patel Institute of Technology. name.  Example : fscanf(f2. constants and strings.  fseek( ) :  fseek( ) function is used to set the position anywhere in the file . meaning moves forwards. The control string contains input specifications for the items in the list.  Here fp is a FILE pointer associated with the file.  fscanf functions return the EOF when the end of file is reached.  The offset may be positive. “%s %d ”. position).  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). ftell( ) returns -1L (a type long -1).  ftell( ) :  ftell( ) function gives the current position in the file .  So n will give the relative offset (in bytes) of the current position.  In case of an error.  It takes the following form : n = ftell (fp). offset. &age).

m. Piludara Page 100 .m. fseek(fp. And fseek( ) function returns -1 when an error is occurs.2).0). fseek(fp.1).-m.  The offset specifies the number of positions to be moved from the location specified by position. ************************************************************* Computer Department/Sardar Patel Institute of Technology.0L.2). fseek(fp.-m. fseek(fp. fseek(fp. fseek(fp.Computer Programming & Utilization Chapter-12 File Management in C  The following table shows the operations of the fseek( ) function : STATEMENT fseek(fp.1).1).0L.0). 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.0L.

Sign up to vote on this title
UsefulNot useful