0% found this document useful (0 votes)
24 views10 pages

File Management

Uploaded by

farhanshafaut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
24 views10 pages

File Management

Uploaded by

farhanshafaut
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

FILE HANDLING

Adiba Ibnat Hossain


Lecturer, Department of CSE, Premier University
Why files are needed?

❑ When a program is terminated, the entire data is lost. Storing in a file will
preserve your data even if the program terminates.

❑ If you have to enter a large number of data, it will take a lot of time to enter
them all. However, if you have a file containing all the data, you can easily
access the contents of the file using a few commands in C.

❑ You can easily move your data from one computer to another without any
changes.
Types of Files
❑ When dealing with files, there are two types of files you should know about:
1) Text files
2) Binary files

1. Text files
Text files are the normal .txt files. You can easily create text files using any simple text
editors such as Notepad.

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).
File Operations
In C, you can perform four major operations on files, either text or binary:

❑ Creating a new file

❑ Opening an existing file

❑ Closing a file

❑ Reading from and writing information to a file


Working with Files

❑ When working with files, you need to declare a pointer of type file.

❑ A file pointer stores the current position of a read or write within a file.

❑ All operations within the file are made with reference to the pointer.

❑ The data type of this pointer is defined in stdio. h and is named FILE.
FILE *fptr;
Function Names and Operations in File
Handling
❑ fopen() : Creates a new file for use or opens an existing file for use.

❑ fclose() : Closes a file which has been opened for use.

❑ fprintf() : Writes a set of data values to a file.

❑ fscanf() : Reads a set of data values from a file.


File Opening Modes
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.

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
created.
file.
Writing to a text file
❑ Write a C program to write data to a text file:
Takes input from the user:

Writes into the text file :


Reading from a text file
❑ Write a C program to read data from a text file:
Data already stored in the
text file :

Reads data from the file:

You might also like