0% found this document useful (0 votes)
7 views

File in C Language

File handling in C allows programs to perform operations on files stored in the local file system such as creation, opening, reading, writing, and deletion. The main functions used are fopen() to open a file, fread() and fscanf() to read from files, fwrite() and fprintf() to write to files, and fclose() to close files. Files can be opened in different modes like read, write, append, etc and the behavior differs based on if the file already exists or not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

File in C Language

File handling in C allows programs to perform operations on files stored in the local file system such as creation, opening, reading, writing, and deletion. The main functions used are fopen() to open a file, fread() and fscanf() to read from files, fwrite() and fprintf() to write to files, and fclose() to close files. Files can be opened in different modes like read, write, append, etc and the behavior differs based on if the file already exists or not.
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

File in C Language

File handling in C enables us to create, update, read, and delete the files stored on the
local file system through our C program. The following operations can be performed on
a file.

o Creation of the new file


o Opening an existing file
o Reading from the file
o Writing to the file
o Deleting the file

Types of Files
1. Text files

2. Binary files

1. Text files

Text files are the normal .txt files.


2. Binary files

Binary files are mostly the .bin files in your computer.


Instead of storing data in plain text, they store it in the binary form (0's and
1's).
Function of File Handling

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

Opening Modes in Standard I/O

Mode Meaning of Mode During Inexistence of file

If the file does not exist, fopen()


r Open for reading.
returns NULL.

If the file does not exist, fopen()


rb Open for reading in binary mode.
returns NULL.
Opening Modes in Standard I/O

Mode Meaning of Mode During Inexistence of file

If the file exists, its contents are


overwritten.
w Open for writing.
If the file does not exist, it will be
created.

If the file exists, its contents are


overwritten.
wb Open for writing in binary mode.
If the file does not exist, it will be
created.

Open for append. If the file does not exist, it will be


a
Data is added to the end of the file. created.

Open for append in binary mode. If the file does not exist, it will be
ab
Data is added to the end of the file. created.

If the file does not exist, fopen()


r+ Open for both reading and writing.
returns NULL.

Open for both reading and writing in If the file does not exist, fopen()
rb+
binary mode. returns NULL.

If the file exists, its contents are


overwritten.
w+ Open for both reading and writing.
If the file does not exist, it will be
created.

If the file exists, its contents are


Open for both reading and writing in overwritten.
wb+
binary mode. If the file does not exist, it will be
created.

If the file does not exist, it will be


a+ Open for both reading and appending.
created.

Open for both reading and appending in If the file does not exist, it will be
ab+
binary mode. created.
The fopen function works in the following way.

o Firstly, It searches the file to be opened.


o Then, it loads the file from the disk and place it into the buffer. The buffer is used
to provide efficiency for the read operations.
o It sets up a character pointer which points to the first character of the file.

Syntax:

Create File Pointer Variable

FILE *ptr;

Ptr=fopen(“url”,”mode”);

For Binary file

fread(addressData, sizeData, numbersData, pointerToFile);

fwrite(addressData, sizeData, numbersData, pointerToFile);

You might also like