Module 6&7
Module 6&7
The use of file handling is exactly what the situation calls for.
In order to understand why file handling is important, let us look at a
few features of using files:
•Reusability: The data stored in the file can be accessed, updated,
and deleted anywhere and anytime providing high reusability.
•Portability: Without losing any data, files can be transferred to
another in the computer system. The risk of flawed coding is minimized
with this feature.
•Efficient: A large amount of input may be required for some
programs. File handling allows you to easily access a part of a file using
few instructions which saves a lot of time and reduces the chance of
errors.
•Storage Capacity: Files allow you to store a large amount of data
without having to worry about storing everything simultaneously in a
program.
Types of Files in C
A file can be classified into two types based on the way the file stores the data. They
are as follows:
•Text Files
•Binary Files
Text Files
A text file contains data in the form of ASCII characters and is generally used to
store a stream of characters.
•Each line in a text file ends with a new line character (‘\n’).
•It can be read or written by any text editor.
•They are generally stored with .txt file extension.
•Text files can also be used to store the source code.
2. Binary Files
A binary file contains data in binary form (i.e. 0’s and 1’s) instead of ASCII
characters. They contain data that is stored in a similar manner to how it is stored in
the main memory.
•The binary files can be created only from within a program and their contents can only
be read by a program.
•More secure as they are not easily readable.
•They are generally stored with .bin file extension.
Note: Only fopen(), fclose(), fprintf(), fscanf(), fread() and fwrite() are in your syllabus
fopen() :
We must open a file before it can be read, write, or update. The fopen() function is
used to open a file. The syntax of the fopen() is given below.
C ftell() function :
The ftell() function returns the current file position of the specified stream.
Syntax:
long int ftell(FILE *stream)
Read and Write in a Binary File:
Till now, we have only discussed text file operations. The operations on a binary file are
similar to text file operations with little difference.
Opening a Binary File:
To open a file in binary mode, we use the rb, rb+, ab, ab+, wb, and wb+ access mode in the
fopen() function.
fptr = fopen("filename.bin", "rb");
Parameters:
•ptr: pointer to the block of memory to be written.
The fread() function can be used to read data from a binary file in C. The data is read
from the file in the same form as it is stored i.e. binary form. Syntax:
// Macro Definition
#define LIMIT 5
int main(){
for (int i = 0; i < LIMIT; i++) {
printf("%d \n", i);
}
return 0;
}
File Inclusion :
File inclusion allows you to include external files (header files, libraries, etc.) into the
current program. This is typically done using the #include directive.
There are two ways to include header files,
#include<filename>
#include”filename”
The ‘<‘ and ‘>’ brackets tell the compiler to look for the file in the standard
directory while double quotes ( ” ” ) tell the compiler to search for the header file in
the source file’s directory.
Example:
// Includes the standard I/O library
#include <stdio.h>
int main() {
printf("Hello World");
return 0;
}
Conditional Compilation :
allows you to include or exclude parts of the code depending on certain conditions.
There are the following conditional preprocessor directives: #if, #ifdef, #ifndef,
else, #elif and #endif.
#endif directive is used to close off the #if, #ifdef, and #ifndef opening directives.
Example:
#include <stdio.h>
// Defining a macro for PI
#define PI 3.14159
int main(){
// Check if PI is defined using #ifdef
#ifdef PI
printf("PI is defined\n");
// If PI is not defined, check if SQUARE is defined
#elif defined(SQUARE)
printf("Square is defined\n");
// If neither PI nor SQUARE is defined, trigger an error
#else
#error "Neither PI nor SQUARE is defined"
#endif
// Check if SQUARE is not defined using #ifndef
#ifndef SQUARE
printf("Square is not defined");
// If SQUARE is defined, print that it is defined
#else
printf("Square is defined");
#endif
return 0;
}