Introduction to File Handling in C

  • When data is stored using different types of variables, it is lost when the program exits unless something is done to save it. Thus, File handling in C supports storing data permanently in the created file for future use.
  • File handling in C is responsible for creating files, their proper maintenance, and ultimately the birth of the DBMS concept.

Definition of File Handling in C

  • File handling in C is the process of creating, opening, reading, writing, appending, updating, and closing files using a C program.
  • A file represents a sequence of bytes on the disk where a group of related data is stored, ending with an end-of-file marker.
  • C provides file-handling functions through the header file <stdio.h>.

        Features/Characteristics of File Handling in C

        • File handling provides a way to store data permanently on a secondary storage device such as a hard disk or SSD so that it can be retrieved and used later.
        • File Handling in C means different types of operations applied to C files.
        • File Handling in C includes new file creation, opening, and closing of files, storing data in files permanently, modifying data from files, retrieving data from a file, etc.
        • 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
          • A File can be used to store a large volume of persistent data.
          • The file is created for the permanent storage of data.
          • It is a ready-made structure. In C language, we use a structure pointer of the file type to declare a file.
          • It is a kind of type/structure that holds information about the file.
        • File Pointer
          • It is declared as: FILE *fp.
          • A file pointer is a pointer to a structure that contains information about the file, including its name, the current position of the file, whether the file is being read or written, and whether errors or end of the file have occurred.
          • A file is generally accessed using a file pointer.
          • To access any file, we need to declare a pointer to the FILE structure and then associate it with the particular file. This pointer is referred to as a file pointer.
          • 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 a file control block (FCB) used by the operating system to administer a particular file.

        Need for File Handling in C

        File handling is required for the following reasons:-

        • Permanent Storage of Data: Data stored in variables is lost when a program terminates. Files allow information to be stored permanently.
        • Handling Large Amounts of Data: Large quantities of information cannot always be conveniently maintained in variables or arrays. Files can store a large number of records.
        • Retrieval of Stored Information: Previously saved information can be read from a file whenever it is required by the program.
        • Data Sharing: Information stored in a file can be accessed by different programs, subject to compatible file formats and permissions.
        • Maintaining Records: Files are useful for maintaining student, employee, customer, product, salary, examination, and other records.
        • Updating Existing Information: Existing file records can be modified or updated without requiring all information to be entered again.
        • Backup of Information: Important program data can be saved in files for future reference and backup purposes.

        Advantages of File Handling in C

        File handling provides several advantages in C programming, which are as follows:

        • Data Persistence: Information remains available even after the program has been closed.
        • Large Data Storage: Files can store much larger amounts of data than would normally be practical to keep in program variables.
        • Easy Retrieval: Stored information can be opened and retrieved whenever required.
        • Data Reusability: Data created during one execution of a program can be reused during another execution.
        • Easy Record Management: Records can be added, searched, modified, and deleted using file operations.
        • Sequential and Random Access: C supports both sequential processing and random access to file data.
        • Text and Binary Files: C can work with both human-readable text files and compact binary files.
        • Reduced Re-entry of Data: Once information is saved, the user does not need to enter the same data every time the program runs.

          File Types in C

          [A.] There are two types of files in C, based on storage of contents: (a)Text files and (b) Binary files

          (a.) Text Files

          • A text file stores data in the form of readable characters. The contents of a text file can normally be opened and understood using a text editor such as Notepad.
          • In text files, everything is stored in terms of text, i.e., even if we store a numeric/integer value 23, it will be stored as a 3-byte string – “23”.
          • In a text file, certain character translations may occur. For example, a newline(n) character may be converted to a carriage return, linefeed pair.
          • Examples of text files are student.txt, employee.c, marks.py, etc.For example, a text file may contain 101 Amit 125.32, 105 Rohini 74.00, etc.
          • Some major text file functions are –  fprintf(), fscanf(), fgetc(), fputc(), fgets(), fputs() etc.

          (b.) Binary Files

          • A binary file contains data that was written in the same format used to store it internally in the 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- one byte each for data and one for the null character.
          • Binary files are easier to handle.
          • Its contents are generally not directly readable in a normal text editor.

          [B.] C also has two types of files – (a) Buffered file System and (b) Unbuffered File System

          (a.) 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) to optimize file system access.

          (b.) 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 a file pointer of type FILE to access a particular file, but they use the file descriptors of type integer. They are also called handles.
          • This is referred to as an unbuffered I/O system because the programmer must provide and maintain all disk buffers; the routines do not do it automatically.

          [C.] C also has two types of files, based on file access: (a) Sequential file and (b) Random file.

          (a) Sequential File

          • Sequential access files allow reading the data from the file in a sequential/linear manner, i.e., data can only be read/searched 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 Handling Built-in Functions

              The major File Handling functions are as follows –

              • fopen():
                • This function is used for creating a file or opening an existing file.
                • fopen() is a standard function that is normally used to open a file.
                • In other 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 is 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 that points to the file; i.e., this function returns a file pointer.
                • The syntax is as –
                       FILE *fopen(char *filename, *openingmode);
                  (where filename is the name of the file that is a string of characters that provides 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 the <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 the <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 to a file.
                • List of File Opening Modes –
                  • To do any operation on a file, it must be opened first and then use the desired 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 to a 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 to 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+         – opens for reading and writing from the beginning.
                w+       – opens for reading and writing, overwriting a file.
                 a+        – opens for reading and writing, appending to the 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 *fp);
                  • This function flushes any unwritten data for the 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 the 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 the C feof function is 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 the file (EOF), by moving forward in the file.
                  • It returns the value zero when the 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 the file has been reached; otherwise, it returns zero.
                  • EOF markers 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 a single character at a time from a file.
                  • This is an ANSI C function 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.getc( ) returns an integer value, but only the low-order byte is used.
                  • It returns EOF when the end-of-file is reached.getc( )is defined in <stdio.h> as a macro, not a function.
                • putc():
                  • putc( ) is used to write/put a single character at a time to a file.
                  • This is an ANSI C function 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 a macro, not a function.
                  • The syntax is as follows –
                    int putc(int ch, FILE *stream);
                  • The file pointer indicates the file to write to.
                  • The character ‘ch’ discussed in the syntax above is formally called an integer in the 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 to by the file pointer; if successful otherwise, it returns EOF.
                  • fgetc cannot be implemented as a macro, whereas getc can be.
                  • Syntax : int fgetc(FILE *fp);
                • fputc():
                  • This function is used to write a single character at a time to a given file.
                  • The character that is written inside the text file is specified by the first argument of the function, pointed to by the fp pointer.
                  • After writing a character to the text file, it increments the position of the pointer.
                  • It returns the character pointed to by the file pointer; if successful, otherwise, it 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., a mix of all these, then character input functions are not enough, as the values would be written 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 used for formatted input, i.e., reading a block of data from a file.
                  • This is identical to scanf()  except that the first argument is a file pointer that specifies the file to be read, and the second argument is the format string.
                  • Syntax : int fscanf(FILE *fp, char *format,. . .);
                  • This function returns an integer indicating the number of bytes written.
                • fprintf():
                  • When a file contains data in the form of digits, real numbers, characters, and strings, i.e., a 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 used 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, and the second argument is the format string.
                  • Syntax : int fprintf(FILE *fp, char *format,. . .);
                  • This function returns an integer indicating the number of bytes 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 number of bytes.
                  • It returns -1 if an error is encountered.
                • fread():
                  • This is an input function that reads a block of data(a 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 the C standard library.
                  • This function allows the 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 the case of binary files.
                  • Generally, this function is used to read an array of records from a file.
                • fwrite(): 
                  • This is an output function that writes a block of data(a specific number of bytes) to a file. A block can be a record, a set of records, or an array.
                  • This function is defined in the C standard library.
                  • This function allows the 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 the case of binary files.
                  • Generally, this function is used to write an array of records from a file.
                • lseek() :
                  • The function lseek() is used to move to a specific position in a file.
                  • Its Syntax/Prototype is :
                          long lseek(int fd, long offset, int pos);
                  • This function is 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 is 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) at 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 the 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

                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.