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.

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:

Files

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.

Function Name Use
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:

FILE *fp;

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:

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:

C Online Test


« Previous Tutorial CodesCracker.com »


Liked this post? Share it!