C- Programming
Sujan Karki
    Email: sujan@ioepc.edu.np
     Contact No.: 9819387234
Master’s in Information System Engineering (MscIne) *Ongoing
 Bachelor in Computer Engineering – Purwanchal Campus,IOE
            UNIT 10
    Files and File Handling in C
2
                       File Handling
    ⚫ File is a collection of related data placed on the disk.
      C support the concept of files through which data can
      be stored on the disk or secondary storage device. The
      stored data can be read whenever required.
3
Opening a File
A file must be opened before any I/O operations performed on
that file. The process of establishing a connection between the
program and file is called opening the file.
A structure named FILE is defined in the file stdio.h that contains
all information about the file like name, status, buffer size,
current position, end of file status etc.
4
 …
Closing of file:
The file that was opened using fopen ( ) function must be closed
when no more operations are to be performed
on it. After closing the file, connection between file and program
is broken.
Although all the files are closed automatically when the program
terminates, but sometimes it may be necessary
to close the file by using fclose( ) function.
5
6
    Text mode
    · In text mode every digit or text are stored as a character
    and while reading the content back, the conversion is
    required from character to appropriate format and takes lots
    of space.
    · Character I/O, string I/O, and formatted I/O use text mode.
    · If 3.14159 is stored in character mode file size would be 8
    bytes (counts each character including decimal and EOF).
    Binary mode
    · In binary mode every digit or text is stored in binary format
    and while reading the content no conversion
    is necessary and takes little space.
    · fread ( ) and fwrite ( ) are used in binary mode.
    · If 3.14159 is stored in character mode file size would be 4
    bytes.
7
    ⚫ Input/Output Functions
    The functions used for file input/output are
    1. Character I/O
    a. fputc( )
    This function writes a character to the specified file at the current
    file position and then increments the file position pointer.
    Syntax: fputc(character or char_variable , file_ptr_variable);
    b. fgetc( )
    This function reads a single character from a given file and
    increments the file pointer.
    Syntax: char_variable = fgetc(file_ptr_variable);
    c. getc( ) and putc( )
    The operation of getc( ) and putc( ) are exactly similar to that of
    fgetc( ) and fputc( ) , the only difference is that the former two are
    defined as macros while the latter two are functions.
8
⚫ Input/Output Functions
    2. Integer I/O
    a. putw( )
    This function writes an integer value to the file pointed to by
    file_pointer.
    b. getw( )
    This function returns the integer value from the file associated with
    file_pointer.
    3. String I/O
    a. fputs( )     Note: String is value written in a file represented by ptr_variable
    This function writes the null terminated string pointed by given
    character pointer to a file.
    Syntax: fputs( string, file_ptr_variable);
                             Note: int_value is no of character of string read from a file
    b. fgets()
                             represented by ptr_variable and stored in string_variable
    This function is used to read characters from a file and these
    characters are stored in the string pointed by a character pointer.
    Syntax: fgets( string_variable ,int_value, file_ptr_variable);
9
 ⚫ Input/Output Functions
 4. Formatted I/O
 a. fprintf( )
 This function is same as the printf( ) function but it writes formatted
 data into the file instead of the standard output(screen). This function
 has same parameters as in printf( ) but it has one additional
 parameter which is a pointer of FILE type, that points to the file to
 which the output is to be written.
 Syntax: fprintf(file_ptr_variable, “control_string”, list_varaibales);
 b. fscanf( )
 This function is similar to the scanf( ) function but it reads data from
 file instead of standard input, so it has one more parameter which is
 a pointer of FILE type and it points to the file from which data will be
 read.
 Syntax: fscanf(file_ptr_variable, “control_string”, &list_varaibales);
10
 ⚫ Input/Output Functions
     5. Block Read/Write
     a. fwrite( )
     This function is used for writing an entire block to a given file.
     Syntax: fwrite(&ptr, size_of_array_or_structure,
     number_of_array_or_structure, fptr);
     b. fread( )
     This function is used to read an entire block from a given file.
     Syntax: fread(&ptr, size_of_array_or_structure,
     number_of_array_or_structure, fptr);
     Ptr is the address of array or structure to be written or read.
     Fptr is a file pointer of a file opened in binary mode.
11
 ⚫ Random file processing/accessing
 In this technique the content are stored sequentially and read back
 the content from any position of the file. And it can be performed
 using some function like rewind( ), fseek( ) and ftell( ).
 1. rewind( )
 The rewind( ) function places the file pointer to the beginning of the
 file.
 rewind(file_pointer);
 2. fseek( )
 This function is used for setting the file position pointer at the
 specified byte.
 fseek(file_pointer, offset, mode);
 3. ftell( )
 This function returns the current position of the file position pointer.
 The value is counted from beginning of the file.
    position=ftell(fp); where position is long int.
12
 ⚫ Random file processing/accessing
 fseek(file_pointer, offset, mode);
 a. fseek(ptr, 0, SEEK_SET); moves file pointer to the beginning of
 file.
 b. fseek(ptr, 0, SEEK_END); moves the file pointer at the end of
 file.
 c. fseek(ptr, 10, SEEK_SET); move the file pointer at 10 bytes right
 from the beginning of file.
 d. fseek(ptr, -2, SEEK_CUR); move the file pointer at 2 bytes left
 from current position.
13
     . . . to be continued !!!
14