Introduction to File Handling
Introduction to File Handling
✓ Text file
✓ Binary file
Text file - The user can create these files easily while handling files in C. It stores
information in the form of ASCII characters internally, and when the file is opened, the
content is readable by humans. It can be created by any text editor with a .txt or .rtf (rich
text)extension. Since text files are simple, they can be edited by any text editor like
Microsoft Word, Notepad, Apple Text Edit, etc.
Binary file - It stores information in the form of 0’s or 1’s, and it is saved with the .bin
extension, taking less space. Since it is stored in a binary number system format, it is not
readable by humans. Therefore it is more secure than a text file.
C File Operations
There are different kinds of file operations in C.
✓ Creating a new file
✓ Opening an existing file
✓ Writing data to a file
✓ Reading data from an existing file.
✓ Moving data to a specific location on the file
✓ Closing the file
S.No. Function Description
1 fopen() opens new or existing file
2 fprintf() write data into the file
3 fscanf() reads data from the file
4 fputc() writes a character into the file
5 fgetc() reads a character from file
6 fclose() closes the file
7 fseek() sets the file pointer to given position
8 fputw() writes an integer to file
9 fgetw() reads an integer from file
10 ftell() returns current position
11 rewind() sets the file pointer to the beginning of the file
File Pointer in C
When you want to perform file operations in C like reading from or writing to a file, you
need a way to reference that file within your program. This reference is provided by the
file pointer. The file pointer stores the address of a FILE structure, which contains details
about the file, like its name, its current position, its size, etc.
FILE is a predefined data type in C, defined in the stdio.h header file. It represents a file
type.
pointer_name can be any valid variable name, and it will become the name of the file
pointer.
For example, if you want to declare a file pointer named fptr, you'd write:
FILE *fptr;
Later on, when you open a file, you will use functions like fopen() which
will return a file pointer, and you can assign this to your declared file
pointer:
This fptr can then be used with other functions to perform various
operations on the file "filename.txt".
Mode Description
r opens a text file in read mode
w opens a text file in write mode
a opens a text file in append mode
r+ opens a text file in read and write mode
w+ opens a text file in read and write mode
a+ opens a text file in read and write mode
rb opens a binary file in read mode
wb opens a binary file in write mode
ab opens a binary file in append mode
rb+ opens a binary file in read and write mode
wb+ opens a binary file in read and write mode
ab+ opens a binary file in read and write mode
//open a file
#include<stdio.h>
#include<stdlib.h>
int main()
{
FILE *fptr;
fptr = fopen("example.txt", "r"); // Open the file in read mode
if (fptr == NULL) {
printf("Error in opening the file.\n");
return 1; // Exit the program if file can't be opened
}
printf("File opened successfully.\n");
fclose(fptr); // It's a good practice to close the file after operation
return 0;
}
Create a File in C
#include <stdio.h>
int main() {
FILE *filePtr;
// Attempt to open the file for writing
filePtr = fopen("example.txt", "w");
// Check if the file was opened successfully
if (filePtr == NULL) {
printf("Error opening or creating the file!\n");
return 1;
}
printf("File opened or created successfully.\n");
// Don't forget to close the file
fclose(filePtr);
return 0;
}
int main() {
FILE *fptr;
int num = 42;
// Opening the file in write mode
fptr = fopen("numbers.txt", "w");
// Writing data to the file using different functions
fprintf(fptr, "The number is: %d\n", num);
fputs("This is a test line.\n", fptr);
fputc('A', fptr);
fputw(100, fptr);
return 0;
}
Example 1: Program to Create a File, Write in it, And Close the File
#include <stdio.h>
#include <string.h>
int main()
{
// Declare a pointer for the file
FILE *diaryFile;
return 0;
}
➢ C fprintf() and fscanf()
Example:
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("myfile.txt","r");
while((c=fgetc(fp))!=EOF)
{
printf("%c",c);
}
fclose(fp);
getch();
}
OUTPUT –
myfile.txt
➢ C fseek() function
The fseek() function is used to set the file pointer to the specified
offset. It is used to write data into file at desired location.
Syntax:
int fseek(FILE *stream, long int offset, int whence)
fp = fopen("myfile.txt","w+");
fputs("This is javatpoint", fp);
Syntax:
void rewind(FILE *stream)
Example:
File: file.txt
this is a simple text
File: rewind.c
For Example
#include<stdio.h>
#include<conio.h>
void main(){
FILE *fp;
char c;
clrscr();
fp=fopen("file.txt","r");
while((c=fgetc(fp))!=EOF){
printf("%c",c);
}
rewind(fp);//moves the file pointer at beginning of the file
while((c=fgetc(fp))!=EOF)
{
printf("%c",c);
}
OUTPUT
fclose(fp);
getch();
}
Syntax:
long int ftell(FILE *stream)
Example:
File: ftell.c
For Example
#include <stdio.h> OUTPUT
#include <conio.h>
void main (){
FILE *fp;
int length;
clrscr();
fp = fopen("file.txt", "r");
fseek(fp, 0, SEEK_END);
length = ftell(fp);
fclose(fp);
printf("Size of file: %d bytes", length);
getch();
}
C Preprocessor Directives
The C preprocessor is a micro
processor that is used by compiler to
transform your code before
compilation. It is called micro
preprocessor because it allows us to
add macros.
Note: Proprocessor direcives are
executed before compilation.
1. Object-like Macros
2. Function-like Macros
1. Object-like Macros
The object-like macro is an identifier that is replaced by
value. It is widely used to represent numeric constants.
For example:
#define PI 3.14
Here, PI is the macro name which will be replaced by the
value 3.14.
Function-like Macros
The function-like macro looks like function call.
For example:
#define MIN(a,b) ((a)<(b)?(a):(b))