File Handling in C
File Handling in C
Topperworld.in
File Handling in C
⚫ File handing in C is the process in which we create, open, read, write, and
close operations on a file.
⚫ C language provides different functions such as fopen(), fwrite(), fread(),
fseek(), fprintf(), etc. to perform input, output, and many different C file
operations in our program.
Types of Files in C
1. Text Files
2. Binary Files
©Topperworld
C Programming
1. 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.
©Topperworld
C Programming
Syntax:
FILE *fopen( const char * filename, const char * mode );
©Topperworld
C Programming
◆ The file name (string). If the file is stored at some specific location, then we
must mention the path at which the file is stored. For example, a file name
can be like "c://some_folder/some_file.ext".
◆ The mode in which the file is to be opened. It is a string.
Syntax:
int fclose( FILE *fp );
Syntax:
Syntax:
©Topperworld