Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære 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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2

Spør AI

expand

Spør AI

ChatGPT

Spør om hva du vil, eller prøv ett av de foreslåtte spørsmålene for å starte chatten vår

Awesome!

Completion rate improved to 12.5

bookReading Data from Files

Sveip for å vise menyen

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

Alt var klart?

Hvordan kan vi forbedre det?

Takk for tilbakemeldingene dine!

Seksjon 2. Kapittel 2
some-alt