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 ...

Get C Programming: Visual Quickstart Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.