Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Learn Reading Data from Files | Reading and Writing Data
C File Handling Basics

bookReading Data from Files

Reading data from files is a fundamental part of file handling in C. The two primary functions used for this purpose are fscanf and fread. The fscanf function is designed for reading formatted text data from a file, much like how scanf reads input from the standard input. You use fscanf when you want to extract values such as strings, integers, or floating-point numbers from a text file, following a specific format. On the other hand, the fread function is used for reading raw binary data from a file. With fread, you can read a specified number of bytes into a buffer, making it suitable for handling data that is not in a human-readable text format, such as images or serialized structures.

main.c

main.c

input.txt

input.txt

copy
123456789101112131415161718192021
#include <stdio.h> int main() { FILE *file = fopen("input.txt", "r"); if (file == NULL) { printf("Unable to open file.\n"); return 1; } char name[50]; // Read a string from the file if (fscanf(file, "%49s", name) == 1) { printf("Read from file: %s\n", name); } else { printf("Failed to read data from file.\n"); } fclose(file); return 0; }
Note
Check for End-of-File and Errors

Always check for the end-of-file condition or errors when reading data from a file. Functions like fscanf and fread return values that indicate how much data was successfully read, which helps you detect issues such as reaching the end of the file or encountering read errors.

question mark

Which function is used to read binary data from a file?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2

Ask AI

expand

Ask AI

ChatGPT

Ask anything or try one of the suggested questions to begin our chat

Awesome!

Completion rate improved to 12.5

bookReading Data from Files

Swipe to show menu

Reading data from files is a fundamental part of file handling in C. The two primary functions used for this purpose are fscanf and fread. The fscanf function is designed for reading formatted text data from a file, much like how scanf reads input from the standard input. You use fscanf when you want to extract values such as strings, integers, or floating-point numbers from a text file, following a specific format. On the other hand, the fread function is used for reading raw binary data from a file. With fread, you can read a specified number of bytes into a buffer, making it suitable for handling data that is not in a human-readable text format, such as images or serialized structures.

main.c

main.c

input.txt

input.txt

copy
123456789101112131415161718192021
#include <stdio.h> int main() { FILE *file = fopen("input.txt", "r"); if (file == NULL) { printf("Unable to open file.\n"); return 1; } char name[50]; // Read a string from the file if (fscanf(file, "%49s", name) == 1) { printf("Read from file: %s\n", name); } else { printf("Failed to read data from file.\n"); } fclose(file); return 0; }
Note
Check for End-of-File and Errors

Always check for the end-of-file condition or errors when reading data from a file. Functions like fscanf and fread return values that indicate how much data was successfully read, which helps you detect issues such as reaching the end of the file or encountering read errors.

question mark

Which function is used to read binary data from a file?

Select the correct answer

Everything was clear?

How can we improve it?

Thanks for your feedback!

SectionΒ 2. ChapterΒ 2
some-alt