Curriculum
Course: C basic
Login

Curriculum

C basic

C Introduction

0/1

C Get Started

0/1

C Comments

0/1

C Constants

0/1

C Operators

0/1

C Break and Continue

0/1

C User Input

0/1

C Memory Address

0/1

C Structures

0/1
Text lesson

C Read Files

Read a File

In the preceding chapter, we utilized the ‘w’ and ‘a’ modes within the fopen() function to write to a file.

To read from a file, employ the ‘r’ mode.

Example

FILE *fptr;

// Open a file in read mode
fptr = fopen(“filename.txt”, “r”); 

This will open the filename.txt file for reading.

Reading a file in C involves a few steps, but don’t worry! We’ll walk you through it step-by-step.

Subsequently, we’ll need to create a string of sufficient size to accommodate the content of the file.

For instance, let’s generate a string capable of holding up to 100 characters:

Example

FILE *fptr;

// Open a file in read mode
fptr = fopen(“filename.txt”, “r”);

// Store the content of the file
char myString[100]; 

To extract the content of filename.txt, we can employ the fgets() function.

The fgets() function accepts three parameters:

Example

fgets(myString, 100, fptr); 

  1. The first parameter indicates the location to store the file content, which, in this case, will be in the myString array we’ve just created.
  2. The second parameter determines the maximum amount of data to read, which should align with the size of myString (100).
  3. The third parameter necessitates a file pointer for reading the file (fptr in our example).

Now, we can print the string, resulting in the output of the file’s content:

Example

FILE *fptr;

// Open a file in read mode
fptr = fopen(“filename.txt”, “r”);

// Store the content of the file
char myString[100];

// Read the content and store it inside myString
fgets(myString, 100, fptr);

// Print the file content
printf(“%s”, myString);

// Close the file
fclose(fptr); 

Hello World!

Note: The fgets function reads only the first line of the file. Recall that filename.txt contains two lines of text.

To read each line of the file, you can employ a while loop.

Example

FILE *fptr;

// Open a file in read mode
fptr = fopen(“filename.txt”, “r”);

// Store the content of the file
char myString[100];

// Read the content and print it
while(fgets(myString, 100, fptr)) {
  printf(“%s”, myString);
}

// Close the file
fclose(fptr); 

Hello World!
Hi everybody!

Good Practice 

Attempting to open a non-existent file for reading will result in the fopen() function returning NULL.

Tip: As a best practice, it’s advisable to use an if statement to check for NULL, and print appropriate text instead, particularly when the file does not exist.

Example

FILE *fptr;

// Open a file in read mode
fptr = fopen(“loremipsum.txt”, “r”);

// Print some text if the file does not exist
if(fptr == NULL) {
  printf(“Not able to open the file.”);
}

// Close the file
fclose(fptr); 

If the file is not found, the following text will be printed:

Not able to open the file.

Considering this, we can develop a more sustainable code by revisiting our previous example of “reading a file.”

Example

If the file exists, read and print its contents. If the file does not exist, display a message.

FILE *fptr;

// Open a file in read mode
fptr = fopen(“filename.txt”, “r”);

// Store the content of the file
char myString[100];

// If the file exist
if(fptr != NULL) {

  // Read the content and print it
  while(fgets(myString, 100, fptr)) {
    printf(“%s”, myString);
  }

// If the file does not exist
} else {
  printf(“Not able to open the file.”);
}

// Close the file
fclose(fptr); 

Hello World!
Hi
everybody!