< Prev
Next >

C - Reading a file on Disk

In our last article, we have introduced you to some important disk input/output operations we could perform. In this tutorial, we are going to explain how to read the contents of an existing file. But before we read the content of an existing file present on the disk, we must open it. For this, the 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/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 and gives us a pointer which is pointed at the either the first character position in the file or the last character position in the file to be read, depending on the mode in which we open this file. Let's read the file mode to use to read the contents of a file.

  • Searching for a file in any directory on disk

  • But we could even search for a file in any directory on our disk by specifying its full path with an extra backslash \ in fopen() function, such as -

    FILE *fp = fopen("C:\\Users\\Admin\\Desktop\\C Programs\\File2.txt", "r");

    Please subscribe our social media channels for notifications, we post a new article everyday.

    Decodejava Google+ Page Decodejava Facebook Page  DecodeJava Twitter Page

    Coming Next
    C++ and Python