Advertisement



< Prev
Next >



C - Disk Input/Output Operations




C language allows us to perform important Disk input/output operations such as - Before we could perform any operations on a file present on the disk, we must open it. For this, the C language provides us a structure called FILE structure. Let's know about the FILE structure.




The FILE structure


C language provides us a structure called FILE, which is defined in the header file <conio.h>. The FILE structure allows us to create, read, write or modify the file on disk.

The FILE structure provides us a function named fopen(), using which we could provide location of the file stored on the disk and in return(if the file is found), the fopen() function open the file in a specific mode to let you perform a specific operation.


Advertisement




Syntax of fopen() function


FILE *fopen(const char *filename, const char *mode)

where filename is the name of the file being searched and mode defines the mode in which the file is opened. This mode is a string value, which could take any of the values explained in the table :

File Modes Description
"r"
Searches for the file and opens it in read mode(if the file is found). If the file is not found, NULL is returned and no new file is created.

"w"
Searches for the file and if the file is found, its contents are overwritten. If file is not found, a new file is created. Allows you to write to the file.

"a"
Searches for the file and if the file is found, the pointer to its last character is returned. Allows you to append new data to the file. If file is not found, a new file is created.

"r+"
Searches for the file and opens the file(if the file is found). If the file is not found, NULL is returned and no new file is created. Allows you to read, write and modify the content of file.

"w+"
Searches for the file and if the file is found, its contents are overwritten. If file is not found, a new file is created. Allows you to read and write the content of file.

"a+"
Searches for the file and if the file is found, the pointer to its last character is returned. Allows you to read and append new data to the file. If file is not found, a new file is created.




This tutorial has introduced you to some important disk input/output operations that we can perform on a file and modes in which we can open a file on disk. In the next tutorial, we are going to explain how to read an existing file on the disk and what if the file we are looking for does not exist? So, see you in the next one!




Please share this article -





< Prev
Next >
< putch(), putchar(), puts()
Reading a File >



Advertisement

Please Subscribe

Please subscribe to our social media channels for daily updates.


Decodejava Facebook Page  DecodeJava Twitter Page Decodejava Google+ Page




Advertisement



Notifications



Please check our latest addition

C#, PYTHON and DJANGO


Advertisement