Programs usually need a data structure to store the data they may need during execution. Normally, this data is stored in variables or arrays. The problem with this is that it gets lost as soon as the program finishes execution. This is because they're temporary memory locations.

If you needed your data to be accessible even after running a program, you would have to save it in a file. Files can be either text or binary.

This article focuses on how you can manage text files in C. Here's what you need to know.

Opening a File

First, it's important to know that you must always declare a pointer of type FILE when working with files. This pointer is necessary to enable communication between the file and the program.

 FILE *filePtr; 

To be able to write, read or update a file, you need to first open it. C provides the standard library function fopen() to do that.

Related: How to Check if Two Matrices Are Identical With Programming

This function takes in two string arguments: the file name and the mode. If the file you're opening isn't located in the program directory you're currently in, then you can provide its file path instead of just using the file name.

Look at the syntax below on how to use fopen():

 filePtr = fopen("filename.txt", "mode"); 

The variable filePtr is a pointer of type FILE. The argument mode refers to the way the file should be opened. That is, what functions are expected to be performed on the file.

There are three modes in which you can open a file in C. These modes are r, w, and a. To read a file, use r. To write to a file, use w. To append data at the end of a file, use a.

For example, if you were planning to read the contents of a file called programming.txt, then you would use the statement below:

 filePtr = fopen("programming.txt" , "r"); 

Apart from r, w, and a, there are other extended modes. Below is a discussion of these modes for text files:

r+ and w+: Both of these modes are used to open a file for reading and writing. The difference is that r+ makes fopen() return NULL if that file doesn't exist, while w+ creates the file instead. Another thing to note is that w+ overwrites a file if it exists.

a+: This mode is used to open a file for appending and reading. If the file doesn't exist, then it's created.

Writing and Reading

You can use the functions fprintf and fscanf respectively, to write and read to a file. These two functions are comparable in functionality to the printf and scanf functions in basic I/O.

 #include <stdio.h>
int main(void){

   // pointer declaration
   FILE *filePtr;
   filePtr = fopen("myfile.txt", "w");

   //the message below will be written to myfile.txt
   fprintf(filePtr, "%s", "Files are permanent memory locations.");
}

The code above shows how you can create a file called myfile.txt and then store a message in it. In order to read the contents of the newly created file see the code below.

 #include <stdio.h>
#include <stdlib.h>

int main(void){

   char message[196];
   FILE *filePtr;

   filePtr = fopen("myfile.txt", "r");

   if (filePtr == NULL){
      printf("Error opening the file");

      //program exists if the pointer returns NULL
      exit(1);
  }
  fscanf(filePtr,"%s", &message);

  printf("The message is: %s", message);
  fclose(filePtr);
}

When you compile and run the above code, your output will be Files. You'll notice that this isn't the full message you stored in myfile.txt. Not to worry though. This isn't a bug in the code.

Related: How Do You Find the ASCII Value Of a Character?

When the program encounters a space, it stops reading the file contents. If the message were instead written as Files-are-permanent-memory-locations, then you would get that whole output on your screen.

In both code examples given, you should have noticed the fclose() statement. You must always close a file (text or binary) after using it.

A Look at C++ Programming

Having reached this level of file handling in C, you are likely an above-average user in the language. This also means that you should be looking at more efficient ways of programming.

C++ is the answer. It's the object-oriented version of C. By now you should be folding up your sleeves to learn this trending language.