From the course: C: Data Structures, Pointers, and File Systems

Reading from a file - C Tutorial

From the course: C: Data Structures, Pointers, and File Systems

Start my 1-month free trial

Reading from a file

- [Instructor] To access a file in C, you need a few things. You need a file that exists. You can't read from a file that doesn't exist. You must know the file's name. And for now, assume that the file dwells in the same folder or directory as the program opening the file. You also need the fopen function, which is part of the standard C library. The fopen function is prototyped in the stdio standard input/output header file. It requires two arguments, the first is a filename string the second is a string representing the file mode or how you want to open the file. The two common modes are r for reading and w for writing. The fopen function returns a FILE pointer, also called a file handle. FILE is a typecast for a structure. It's also used by other file access functions to do things with the open file. Here is an example of how to create a FILE pointer variable in your code. While the file is open, you use various C functions to read its contents. These functions are file versions of standard C input/output functions through they may require a file handle as an argument. Here are some functions used to read from files, some of them you may recognize. When you're done with a file, you use the fclose function. It requires the handle of an open file as an argument and this is the final step and it's important. Do not forget to close the file or you may alter the file's contents unintentionally. This code opens the file sonnet18.txt which is included in the exercise files for this course. File handle fh is declared at line six. The file is opened for reading at line nine. If the file handle is null, the open operation failed, for example, the file doesn't exist. A message is displayed and the program quits otherwise the file is closed at line 19, nothing is done with the file but the fclose function takes the fh file handle as its argument, that's how you know it's closed, let's build and run and the file was opened and closed. Not much more to it. So obviously you want to do a little bit more with the file that you opened for reading such as actually read something from it. In this exercise file, the fgetc function at line 20 fetches a single character from the file and stores it in integer variable ch. If that character is the EOF constant tested for at line 21, that means the end of file has been encountered. The endless while loop breaks otherwise the character is output. Let's build and run. And now you see the file's contents. This code also reads from the same file but it uses the fgets function at line 18 to read a line of text or 64 bytes at a time. This information is stored in the character array buffer. The fgets function returns null when no further text is available to read from the file which exists the while loop otherwise the buffer read is output at line 19. Build and run. And the output is the same. The difference between reading a single character or a chunk of text is negligible. File access is buffered by the operating system so whichever method works for you, it's okay.

Contents