O'Reilly logo

C Programming: Visual Quickstart Guide by Marc Liyanage, Larry Ullman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Opening and Closing Files

Working with a file starts by opening it and ends by closing that file. But before doing anything with a file, you must create a variable as a file pointer. The syntax is

FILE *file_pointer_name;

For the sake of simplicity, you'll often see fp used to represent a file pointer:

FILE *fp;

This variable is assigned a value during the process of opening the file for reading or writing. The file pointer will then point to all the information C needs to work with that file.

The functions used to read from and write to files reside in the stdio.h header file, which you're already including in your applications. The first function, fopen(), is used to initialize the file pointer:

fp = fopen (file_name, mode);

The file_name ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required