From the course: Complete Guide to C Programming Foundations

Unlock the full course today

Join today to access over 24,800 courses taught by industry experts.

Reading from a file

Reading from a file

- [Instructor] To access or open a file in C, you must know that the file exists. You must know the file's name. For now, assume that the file dwells in the same directory as the program opening the file. To make all this magic happen, you take these details and you use the fopen function to open the file. The app open function is prototyped in the stdio.h header file. It requires two arguments. The first is the file name, a string. The second is a string representing the file mode or how to open the file. Two common file modes are R for reading and W for writing. The fopen function returns a file pointer, which is also called a file handle. The file pointer variable is declared in this manner, with handle as the variable name. You must test the file pointer against the null constant to ensure that the file was open properly. If the values aren't equal, the file handle references a structure used by other file access functions to do things with the open file. After it's opened, you…

Contents