- C Programming
- C Tutorial
- C Basic Syntax
- C Data Types
- C if...else Statement
- C switch Statement
- C for Loop
- C while Loop
- C do...while Loop
- C Jump Statements
- C Arrays
- C Strings
- C Pointers
- C Functions
- C Library Functions
- C Recursion
- C Variable Scope
- C Structures
- C Linked List
- C Stacks
- C Queues
- C Binary Tree
- C Header Files
- C File I/O
- C Programming Examples
- C Programming Examples
C File I/O | File Handling in C with Examples
This tutorial will teach you everything you need to know about the C file system (file handling in C), from beginner to advanced. Let's start with streams and files.
Streams and Files
Before going on to discuss the C file system, it is necessary to know about the term "streams" and "files," or the difference between streams and files. The C I/O system supplies a consistent interface to the programmer independent of the actual device being accessed, i.e., the C I/O system provides a level of abstraction between the programmer and the device, and this abstraction is called a "stream," and the actual device is called a "file." Let's discuss streams.
The C file system is intended for use with a wide range of devices, such as terminals, disk drives, and tape drives. However, each device is distinct, and the buffered file system converts each into a logical device known as a stream. Streams are classified into two types:
- Text Streams: A text stream is a sequence of characters.
- Binary Streams: A binary stream is a sequence of bytes that has a one-to-one correspondence to the bytes in the external device, i.e., no character translation occurs.
In C, a file may be anything from a disk file to a terminal or printer. You can associate a stream with a specific file by performing an open operation. And once a file is opened, information can be exchanged between it and your program.
C File Handling Functions
The C file system is made up of several interconnected functions. The most common of these functions are listed in the table below. The header file "stdio.h" is required for these functions.
|fopen()||Opens a file|
|fclose()||Closes a file|
|putc()||Writes a character to a file|
|fputc()||Same as putc()|
|getc()||Reads a character from a file|
|fgetc()||Same as getc()|
|fgets()||Reads a string from a file|
|fputs()||Writes a string to a file|
|fseek()||Seeks a specified byte in a file|
|ftell()||Returns the current file position|
|fprintf()||It is similar to "printf()," but it operates on files|
|fscanf()||It is similar to "scanf()," but it operates on files|
|feof()||If the end of the file is reached, this function returns true|
|ferror()||Returns true if an error has occurred|
|rewind()||Resets the file position indicator to the beginning of the file|
|remove()||Erases a file|
|fflush()||Flushes a file|
The File Pointer
The file pointer is simply the common thread that unites the C I/O system. A file pointer is a pointer to a structure of type FILE. It points to the information that defines various things about the file, including its name, status, and current position type FILE. It points to the information that defines various things about the file, including its name, status, and current position. Here is the statement to use the file pointers in your C program:
Opening a File in C
In C, use the fopen() function to open a file. The fopen() function creates a stream and associates it with a file. The file pointer associated with that file is then returned. The prototype of the fopen() function is shown below.
FILE *fopen(const char *filename, const char *mode);
"filename" is the name of the file, and "mode" is the mode of file access. Here is a list of all the access modes that can be used when opening your file: Here, the filename is the name of the file, and mode is the file access mode. Here is the list of all access modes that you can use to open your file:
- r: open an existing text file for reading
- w: create a text file for writing
- a: append to a text file
- rb: open a binary file for reading
- wb: create a binary file for writing
- ab: append to a binary file
- r+: open a text file for reading and writing
- w+: create a text file for reading and writing
- a+: append or create a text file for reading and writing
- r+b: open a binary file for reading and writing
- w+b: create a binary file for reading and writing
- a+b: append or create a binary file for reading and writing
For example, the following code fragment uses the fopen() function to open a file named myfile.txt for reading:
FILE *fp; fp = fopen("myfile.txt", "r");
Closing a File in C
In C, use the fclose() function to close a file. This function closes a stream that was opened by a fopen() call. The fclose() function prototype is:
int fclose(FILE *fp);
In this case, fp is the file pointer returned by the function fopen().
Writing a Character to a File in C
The C I/O system defines the following two functions that are used in outputting a character to a file: Both functions are equivalent.
Here is the prototype of the function putc().
int putc(int ch, FILE *fp);
"ch" is the character to be output. If a putc() operation succeeds, it returns the character that was written. Otherwise, EOF is returned (end-of-file).
Reading a Character from a File in C
The C I/O system defines the following two functions that are used in inputting a character from a file: Both functions are equivalent.
Here is the prototype of the function getc().
int getc(FILE *fp);
When the file's end is reached, the function getc() returns an EOF.
C File Handling Example Programs
The following is a list of example programs for working with files in C:
- Read a file in C.
- Write the content into a file using a C program.
- Copy the content of one file into another using a C program.
- merge the content of two files using a C program.
- Reverse the content of a file using a C program.
- Count all the characters in a file using a C program.
- List all files in a directory using a C program.
- Encrypt and decrypt the content of a file using a C program.
- Delete a file using a C program.
« Previous Tutorial CodesCracker.com »