0% found this document useful (0 votes)
2 views

2C Read to a File_082922

This document explains how to read files in C using the fopen() function with the 'r' mode. It provides examples of reading a file's content into a string using the fgets() function and demonstrates how to print the content. Additionally, it shows how to read multiple lines from a file using a while loop.

Uploaded by

darrylyann5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

2C Read to a File_082922

This document explains how to read files in C using the fopen() function with the 'r' mode. It provides examples of reading a file's content into a string using the fgets() function and demonstrates how to print the content. Additionally, it shows how to read multiple lines from a file using a while loop.

Uploaded by

darrylyann5
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

C Read Files

Read a File
In the previous chapter, we wrote to a file using w and a modes inside
the fopen() function.

To read from a file, you can use the r mode:

Example
FILE *fptr;

// Open a file in read mode


fptr = fopen("filename.txt", "r");

This will make the filename.txt opened for reading.

It requires a little bit of work to read a file in C. Hang in there! We will guide
you step-by-step.

Next, we need to create a string that should be big enough to store the
content of the file.

For example, let's create a string that can store 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];

In order to read the content of filename.txt, we can use the fgets() function.

The fgets() function takes three parameters:

Example
fgets(myString, 100, fptr);
1. The first parameter specifies where to store the file content, which will
be in the myString array we just created.
2. The second parameter specifies the maximum size of data to read,
which should match the size of myString (100).
3. The third parameter requires a file pointer that is used to read the file
(fptr in our example).

Now, we can print the string, which will output the content of the file:

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);

#include <stdio.h>

int main() {
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 file content


printf("%s", myString);
// Close the file
fclose(fptr);

return 0;
}

Note: The fgets function only reads the first line of the file. If you
remember, there were two lines of text in filename.txt.

To read every line of the file, you can use 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);

You might also like