Reading from and Writing to Files in C: A Fun, Free, & Easy C Tutorial

Hello there, curious coder! Ever felt lost when it comes to handling files in C? Well, you’re in the right place. Today, we’re going to dive into the world of files and learn how to read from and write to them using C. So, buckle up and let’s get started!

Understanding Files in C

In C, a file is a place where you can store data. It’s like a huge storage box where you can keep your data safe and sound. But how do we interact with this box? That’s where functions come in.

The Magic of Functions

Functions are the equivalent of keys that open the box. They allow us to access the data stored in the files. Some of the most commonly used functions for file handling in C are fopen, fclose, fscanf, and fprintf.

The fopen Function

The fopen function is used to open a file. It’s like the key that unlocks the box. Here’s how it works:

FILE *fptr;
fptr = fopen("file.txt", "r");

In the above code, FILE is a type defined in stdio.h. fptr is a file pointer that points to the file we want to open. The fopen function takes two arguments: the name of the file and the mode in which we want to open the file.

The fclose Function

Once we’re done with the file, we need to close it. That’s where the fclose function comes in. It’s like the key that locks the box back up. Here’s how you can use it:

fclose(fptr);

The fscanf and fprintf Functions

The fscanf and fprintf functions are used to read from and write to the file. They’re like the hands that reach into the box to get or put something. Here’s how you can use them:

// Writing to the file
fprintf(fptr, "Hello, World!");

// Reading from the file
char str[50];
fscanf(fptr, "%s", str);
C

In the above code, fprintf writes the string “Hello, World!” to the file pointed to by fptr. fscanf, on the other hand, reads a string from the file and stores it in the str array.

Code Examples

Now that we’ve got the basics down, let’s look at some complete examples of reading from and writing to files in C.

Example 1: Writing to a File

#include <stdio.h>

int main() {
    FILE *fptr;
    fptr = fopen("file.txt", "w");

    if (fptr == NULL) {
        printf("Error!");
        return 1;
    }

    fprintf(fptr, "Hello, World!");
    fclose(fptr);

    return 0;
}
C

In this example, we first open a file named “file.txt” in write mode. If the file cannot be opened, we print an error message and return 1. Otherwise, we write “Hello, World!” to the file and then close it.

Example 2: Reading from a File

#include <stdio.h>

int main() {
    FILE *fptr;
    char str[50];

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

    if (fptr == NULL) {
        printf("Error!");
        return 1;
    }

    fscanf(fptr, "%s", str);
    printf("Read from file: %s\n", str

", str);

    fclose(fptr);

    return 0;
}
C

In this example, we first open a file named “file.txt” in read mode. If the file cannot be opened, we print an error message and return 1. Otherwise, we read a string from the file, print it, and then close the file.

Wrapping Up

Reading from and writing to files in C doesn’t have to be a daunting task. With the right functions and a bit of practice, you can easily handle files in your C programs. Remember, practice makes perfect. So, don’t be afraid to get your hands dirty and write some code!

Frequently Asked Questions (FAQ)

  1. How to read and write into file in C?

    You can use the fscanf and fprintf functions to read from and write to a file in C.

  2. How can we read from and write to file?

    You can read from and write to a file in C by using the fopen function to open the file, the fscanf and fprintf functions to read from and write to the file, and the fclose function to close the file.

  3. How to read text from a text file in C?

    You can use the fscanf function to read text from a text file in C.

  4. How to read a file and copy to another file in C?

    You can read a file and copy it to another file in C by reading the data from the source file using fscanf, and then writing the data to the destination file using fprintf.

  1. Introduction to File Handling in C
  2. File Modes and Operations in C
  3. Working with Binary Files in C
  4. Working with Text Files in C
Scroll to Top