Introduction

  • When data is stored using variables, the data is lost/disappear when the program exits, unless something is done to save it.

Definition

  • A File is a sequential stream of bytes ending with an end-of-file marker. 
  • file represents a sequence of bytes on the disk where a group of related data is stored. 

Features/Characteristics

  • Each file ends either with an end-of-file marker or at a specified byte number recorded in a system maintained, administrative data structure.
  • FILE
    • It is a kind of type/structure that holds information about the file.
    • A File can be used to store a large volume of persistent data.
    • File is created for permanent storage of data.
    • It is a readymade structure. In C language, and we use a structure pointer of file type to declare a file.
  • File Pointer 
    • file pointer is a pointer to a structure, which contains information about the file, including its name, current position of the file, whether the file is being read or written, and whether errors or end of the file have occurred.

File Structure

  • Streams of File – 
    • Streams provide communication channels between files and programs.
    • When the file is opened, the stream is associated with the file.
    • By default, three files and their streams are automatically opened when program execution begins – the standard input, standard output, and the standard error.
    • The standard input, standard output and the standard error are manipulated using file pointers stdin, stdout and stderr.
    • The standard input stream enables a program to read data from the keyboard.
    • The standard output stream enables to write data on the screen.
  • Buffered File System –
    • This file system is referred to as buffered because, the routines maintain all the disk buffers required for reading / writing automatically.

File Types

  • C has two types of files,on the basis of storage of contents – (A) Binary files and (B)Text files.
    • Binary Files –
      • A binary file contains data that was written in the same format used to store internally in main memory. For example, the integer value 1500 will be stored in 2 bytes depending on the machine while it will require 5 bytes in a text file.
      • The binary files are easier to handle.
      • In binary files, no special string to numeric conversions is necessary.
    • Text Files –
      • In text files, everything is stored in terms of text i.e. even if we store an numeric/integer value 23, it will be stored as a 3-byte string – “23\0”.
      • In a text file certain character translations may occur. For example a newline(\n) character may be converted to a carriage return, linefeed pair.
  • C also has two types of files – (I) Buffered file System and (II) Unbuffered file System
(I) Buffered file System
      • The buffered file system uses buffered input and output i.e. the operating system handles the details of data retrieval and storage,
      • The system stores data temporarily (in buffers) in order to optimize file system access.
(II) Unbuffered file System
      • The unbuffered I/O functions are handled directly as system calls without buffering the data by the operating system. That is why they are also known as low level functions.
      • The low level functions are defined in the header file <io.h>.
      • These functions do not use file pointer of type FILE to access a particular file, but they use directly the file descriptors, of type integer. They are also called handles.
      • This is referred to as unbuffered I/O system because the programmer must provide and maintain all disk buffers, the routines do not do it automatically.
  • C also has two types of files, on the basis of file access – (a) Sequential file and (b) Random file
(a) Sequential File
      • Sequential access files allow reading the data from the file in sequential/linear manner i.e. data can only be read/search in sequence.
(b) Random File
      • Random access files allow reading/searching data from any location in the file.
      • To do this, C defines a set of functions(such as fseek(), rewind(), ftell() etc.) to manipulate the position of the file pointer.

File Pointer

  • To access any file, we need to declare a pointer to FILE structure and then associate it with the particular file. This pointer is referred as a file pointer.
  • It is declared as –
       FILE *fp;
  • Opening a file returns a pointer to a FILE structure (defined in <stdio.h>) that contains information, such as size, current file pointer position, type of file etc., to perform operations on the file.This structure also contains an integer called a file descriptor which is an index into the table maintained by the operating system namely, the open file table. Each element of this table contains a block called file control block (FCB) used by the operating system to administer a particular file.

File Handling Functions

  • fopen()
    • To do any operation in a file, it must be open first.
    • This function is used for creating a file or opening an existing file.
    • fopen() is a standard function which is normally used to open a file. In another words, If the specified file is not present on the system, then it is created and then opened and when the specified file is already present on the system, then it directly opened using this function.
    • The fopen() function opens a stream for use and links a file with that stream.
    • fp is a file pointer which points to the type file i.e. this function returns a file pointer.
    • The syntax is as –
           FILE *fopen(char *filename, *openingmode);
      (where filename is the name of file that is a string of characters that provide a valid file name for the operating system and may include a path specification and opening mode is a string, containing the desired open status.)
    • The value returned by the fopen( ) function is a file pointer.
    • If any error occurs while opening the file, the value of the file pointer is NULL, a constant declared in <stdio.h> file.
    • After opening the file, the next thing needed is the way to read or write the file.
    • There are several functions and macros defined in <stdio.h> header file for reading and writing the file. These functions can be categorized according to the form and type of data read or written on to a file. The list of several file functions are discussed in this topics. 
    • List of File Opening Mode –
File Opening Mode   –      Details
r / rt       –   opens a text file for read only mode/process.
w / wt    –   creates a text file for write only mode/process.
a / at      –   appends a file to text file. 
r+t          –   opens a text file for read and write mode/process both.
w+t        –   creates a text file in read and write mode.
a+t         –   opens/creates a text file in read mode.
rb           –   opens a binary file in read only mode.
wb         –   creates a binary file in write only mode.
ab          –  appends a file in a binary file. 
r+b        –   opens a binary file in read and write mode.
w+b      –   creates a binary file in read and write mode.
a+b       –   opens a binary file in read mode.
                            r+         –    open for reading and writing from beginning.
                            w+       –    open for reading and writing, overwriting a file.
                            a+        –    open for reading and writing, appending to file.
  • fclose()
    • It is suggested that an open file must be closed properly when the processing of the file is finished.
    • Syntax is :
              int fclose(FILE *fptr);
    • This function flushes any unwritten data for stream, discards any unread buffered input, frees any automatically allocated buffer, and then closes the stream.
    • It returns 0 if the file is closed successfully or a constant EOF, an end-of file marker, if an error occurred. This constant is also defined in <stdio.h>.
    • If the function fclose() is not called explicitly, the operating system normally will close the file when the program execution terminates.
    • Once the file is closed, it cannot be used further. If required it can be opened again in same or another mode.
  • feof()
    • It is the C library file handling function that checks/tests/finds the end-of-file indicator for the given stream for a file.
    • The purpose of C feof function is used to determine whether the end of the file (stream), specified has been reached or not. 
    • This function keeps on searching from the beginning till the end of file (eof), by moving forward, in our file program.
    • It returns the value zero when end of the file has not occurred, otherwise it returns 1.
    • Syntax :

int feof(FILE *stream/*filename);

  • eof() [End of file]
    • EOF is an end-of-file marker.
    • It is a macro defined in <stdio.h>.
    • It is a macro definition of type int that expands into a negative integral constant expression (generally, -1) i.e. Its value is –1.
    • It returns non-zero when the end of file has been reached, otherwise it returns zero.
    • EOF marks are helpful in data transmission and storage. Files are stored in blocks, and the end marker helps the computer to know it has allocated enough space to store the file.
  • getc()
    • getc( ) is used to read single character at a time from a file.
    • This  is an ANSI C functions for reading character by character or one byte at a time from a file.
    • This function is defined in the standard library i.e. in <stdio.h> as macros not functions.
    • The syntax is as follows –     
           int getc(FILE *stream);
    • The file pointer indicates the file to read from it.
    • getc( ) returns an integer value but only the low order byte is used.
    • It returns EOF when end-of-file is reached.
    • getc( )is defined in <stdio.h> as macros not functions.
  • putc()
    • putc( ) is used to write/put single character at a time to a file.
    • This  is an ANSI C functions for writing character by character or one byte at a time into a file.
    • This function is defined in the standard library i.e. in <stdio.h> as macros not functions.
    • The syntax is as follows –
      int putc(int ch, FILE *stream);
    • The file pointer indicates the file to write into it.
    • The character ‘ch’ is discussed in syntax above is formally called an integer in putc( ) function but only the low order byte is used. On success putc( ) returns a character(in integer form) written or EOF on failure.
    • putc( ) is defined in <stdio.h> as macros not functions.
  • putw()
    • Writing an integer to a file.
  • getw()
    • Reads an integer from a file.
  • fgetc()
    • This function is used to read a character from a file at a time.
    • After reading a character it moves the file pointer position to the next address/ location to read the next character.
    • It returns the read character pointed by file pointer, if successful otherwise, returns EOF.
    • fgetc cannot be implemented as a macro whereas getc can do.
    • Syntax :

int fgetc(FILE *fp);

  • fputc()
    • This function is used to write a single character at a time to a given file.
    • The character which is written inside the text file is specified by the first argument of the function pointed by the fp pointer.
    • After writing a character to the text file, it increments the position of pointer.
    • It returns the write character pointed by file pointer, if successful otherwise, returns EOF.
    • Syntax:

int fputc(int ch, FILE *fp);

  • fgets()
    • This file handling string function is used to read a whole line in the file.
    • C has some string input functions with the help of which we can read a set of characters at one time.
    • This function is defined in the C standard library.
    • Syntax :

char *fgets(char *str, int num, FILE *stream);

(Where the integer parameter in fgets( ) (int num) is used to indicate that at most num -1 characters are to be read from the file, terminating at end-of-file or end-of-line. The end-of-line character will be placed in the string str before the string terminator, if it is read. If end-of-file is encountered as the first character, EOF is returned, otherwise str is returned.)

  • fputs()
    • This file handling string function is used to write a whole line in the file.
    • C has some string output functions with the help of which we can write a set of characters at one time.
    • This function is defined in the C standard library.
    • Syntax :

int fputs(char *str, FILE *stream);

(Where the end-of-line character will be placed in the string str before the string terminator, if it is read. If end-of-file is encountered as the first character, EOF is returned, otherwise str is returned. The fputs( ) function returns a non-negative number or EOF if unsuccessful.)

  • fscanf()
    • When a file contains data in the form of digits, real numbers, characters and strings i.e. mix of all these, then character input functions are not enough as the values would be write in the form of characters. Hence C provides a specialized formatted input function called fscanf().
    • These are defined in the C standard library.
    • This function is applied for formatted input i.e. reading a block data from a file.
    • This is identical to scanf()  except that the first argument is a file pointer that specifies the file to be written, the second argument is the format string.
    • Syntax :

int fscanf(FILE *fp, char *format,. . .);

    • This function returns an integer indicating the number of bytes actually written.
  • fprintf()
    • When a file contains data in the form of digits, real numbers, characters and strings i.e. mix of all these, then character read functions are not enough as the values would be read in the form of characters. Hence C provides a specialized formatted output function called fprintf().
    • These are defined in the C standard library.
    • This function is applied for formatted output i.e. writing a block of data to a file.
    • This is identical to printf() except that the first argument is a file pointer that specifies the file to be read, the second argument is the format string.
    • Syntax :

int fprintf(FILE *fp, char *format,. . .);

    • This function returns an integer indicating the number of bytes actually read.
  • read()
    • The read() function reads data previously written to a file.
  • write()
    • The write function returns the number of bytes successfully written into the array, which may at times be less than the specified no.of bytes.
    • It returns -1 if an error is encountered.
  • fread()
    • This is an output function that reads a block of data(specific number of bytes) from a file. A block can be a record, a set of records or an array.
    • This function is defined in C standard library.
    • This function allows reading of blocks of data.
    • Syntax is :
         int fread(void *buf, int num_bytes, int count, FILE *fp);

(Where buf is the pointer to a memory area that receives the data from the file, num_bytes specifies the number of bytes to be read.)

    • This function is quite helpful in case of binary files.
    • Generally this function is used to read array of records from a file.
  • fwrite()
    • This is an input function that writes a block of data(specific number of bytes) from a file. A block can be a record, a set of records or an array.
    • This function is defined in C standard library.
    • This function allows writing of blocks of data.
    • Syntax :

int fwrite(void *buf, int num_bytes, int count, FILE *fp);

(Where buf is the pointer to the information to be written to the file, num_bytes specifies the number of bytes to be written.)

    • This function is quite helpful in case of binary files.
    • Generally this function is used to write array of records from a file.
  • lseek()
    • The function lseek() is used to move to the specific position in a file.
    • Its Syntax/Prototype is :
            long lseek(int fd, long offset, int pos);
    • This function is exactly the same as fseek() except that the file descriptor is used instead of the file pointer.
    • It is possible to write any kind of program dealing with files.
  • fseek()
    • To support random access files, C requires a function with the help of which the file pointer can be positioned at any random location in the file. Such a function defined in the C standard library.
    • The function fseek( ) is used to set the position of a file pointer to a specified location.
    • Its Syntax/Prototype is :
                int fseek(FILE *fp, long offset, int pos);
      (Where the first argument fp is the pointer to a file. The second argument is the number of bytes to move the file pointer, counting from zero. This argument can be positive, negative or zero depending on the desired movement. The third parameter is a flag indicating from where in the file to compute the offset. It can have three values :-
            SEEK_SET(or value 0) the beginning of the file,
            SEEK_CUR(or value 1) the current position and
            SEEK_END(or value 2) the end of the file

These three constants are defined in <stdio.h>.)

    • If process is successful fseek( ) returns zero.
  • ftell()
    • The function ftell() is used to give the current position of a file pointer.
    • Its Syntax/Prototype is :
             long ftell(FILE *fp);
    • It returns –1 on error and the position of the file pointer if successful.
  • rewind()
    • The function rewind() is used to reset the file pointer position to the beginning of the file.
    • Its syntax/prototype is :
           void rewind(FILE *fp);
    • A call to rewind is equivalent to the call
                fseek(fp,0,SEEK_SET);

Loading

Categories: C

0 Comments

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.