C Programming: Text File vs Binary File

beginner
19 min

C Programming: Text File vs Binary File

Welcome to a comprehensive guide on C Programming, focusing on Text Files and Binary Files! We'll walk through the differences, use cases, and practical examples to help you understand and master these essential file handling concepts.

Understanding Files in C Programming 📝

A file is a collection of data stored on your computer. In C, files are treated as streams, and you can read from or write to them using various functions.

There are two types of files:

  1. Text Files (also known as ASCII files)
  2. Binary Files

Let's explore each type in detail!

Text Files 📝

Text files store data in plain text, using the ASCII encoding system. In C, text files can be opened using the fopen() function, and their contents can be read, written, or appended using functions like fgets(), fprintf(), and fputs().

Practical Example - Writing to a Text File 💡

c
#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char *filename = "example.txt"; char data[] = "Hello, World!"; // Open the file in write mode ("w") and create it if it doesn't exist. file = fopen(filename, "w"); if (file == NULL) { printf("Error opening file.\n"); exit(1); } // Write data to the file. fprintf(file, "%s\n", data); // Close the file. fclose(file); printf("Data written to file successfully.\n"); return 0; }

This example opens a file called example.txt in write mode, writes "Hello, World!" to it, and closes the file.

Reading from a Text File 💡

c
#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char *filename = "example.txt"; char buffer[100]; // Open the file in read mode ("r"). file = fopen(filename, "r"); if (file == NULL) { printf("Error opening file.\n"); exit(1); } // Read data from the file into a buffer. fgets(buffer, sizeof(buffer), file); // Print the data. printf("Data from the file: %s", buffer); // Close the file. fclose(file); return 0; }

This example opens the file example.txt in read mode, reads its contents into a buffer, and prints the data.

Binary Files 💡

Binary files store data in a binary format, which is not human-readable. Binary files can store complex data structures such as images, audio, or serialized objects, making them essential for many applications. In C, binary files can be opened using the fopen() function, and their contents can be read, written, or appended using functions like fread(), fwrite(), and fseek().

Practical Example - Writing to a Binary File 💡

c
#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char *filename = "example.bin"; int data = 42; // Open the file in write mode ("wb"). file = fopen(filename, "wb"); if (file == NULL) { printf("Error opening file.\n"); exit(1); } // Write an integer to the file. fwrite(&data, sizeof(data), 1, file); // Close the file. fclose(file); printf("Data written to file successfully.\n"); return 0; }

This example opens a file called example.bin in write mode, writes an integer 42 to it, and closes the file.

Reading from a Binary File 💡

c
#include <stdio.h> #include <stdlib.h> int main() { FILE *file; char *filename = "example.bin"; int data; // Open the file in read mode ("rb"). file = fopen(filename, "rb"); if (file == NULL) { printf("Error opening file.\n"); exit(1); } // Read an integer from the file. fread(&data, sizeof(data), 1, file); // Print the data. printf("Data from the file: %d", data); // Close the file. fclose(file); return 0; }

This example opens the file example.bin in read mode, reads an integer from it, and prints the data.

Quiz Time 🎯

Quick Quiz
Question 1 of 1

What are the two main types of files in C Programming?

That's it for this guide! We've covered the basics of text files and binary files in C Programming, and you've seen practical examples of writing and reading data for both types.

Happy coding, and remember to check out more C Programming tutorials on CodeYourCraft! 🎉