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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2

Pregunte a AI

expand

Pregunte a AI

ChatGPT

Pregunte lo que quiera o pruebe una de las preguntas sugeridas para comenzar nuestra charla

Suggested prompts:

Can you give examples of how to use fscanf and fread in C?

What are the main differences between fscanf and fread?

When should I use fscanf instead of fread?

Awesome!

Completion rate improved to 12.5

bookReading Data from Files

Desliza para mostrar el menú

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

¿Todo estuvo claro?

¿Cómo podemos mejorarlo?

¡Gracias por tus comentarios!

Sección 2. Capítulo 2
some-alt