File Modes and Operations in C: An Easy, Fun, & Free C Tutorial

Hello there, curious coder! If you’ve ever felt lost when it comes to file modes and operations in C, you’re in the right place. Today, we’re going to dive into the world of files and learn how to manipulate them using different modes and operations in C. So, buckle up and let’s get started!

Understanding File Modes in C

In C, a file mode determines how we can interact with a file. It’s like a set of rules that governs what we can and can’t do with a file. There are several file modes in C, including r, w, a, r+, w+, and a+.

The ‘r’ Mode

The ‘r’ mode opens a file for reading only. The file must already exist. Here’s an example:

#include <stdio.h>

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

    if (fptr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    printf("File opened in 'r' mode successfully!\n");
    fclose(fptr);

    return 0;
}
C

The ‘w’ Mode

The ‘w’ mode opens a file for writing only. If the file exists, its contents will be destroyed. If it does not exist, a new file will be created. Here’s an example:

#include <stdio.h>

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

    if (fptr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    printf("File opened in 'w' mode successfully!\n");
    fclose(fptr);

    return 0;
}
C

The ‘a’ Mode

The ‘a’ mode opens a file for appending. Data will be added to the end of an existing file. If the file does not exist, a new file will be created. Here’s an example:

#include <stdio.h>

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

    if (fptr == NULL) {
        printf("Error opening file!\n");
        return 1;
    }

    printf("File opened in 'a' mode successfully!\n");
    fclose(fptr);

    return 0;
}
C

The ‘r+’, ‘w+’, and ‘a+’ Modes

The ‘r+’ mode opens a file for both reading and writing. The file must already exist. The ‘w+’ mode opens a file for both reading and writing. If the file exists, its contents will be overwritten. If it does not exist, a new file will be created. The ‘a+’ mode opens a file for both reading and appending. If the file does not exist, a new file will be created.

Understanding File Operations in C

File operations in C allow us to interact with files. We can perform various operations on files, such as reading from a file, writing to a file, and closing a file. These operations are performed using functions such as fopen, fread, fwrite, and fclose.

Code Examples

Now that we’ve got the basics down, let’s look at some complete examples of file modes and operations 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 opening file!\n");
        return 1;
    }

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

    printf("Written to file successfully!\n");

    return 0;
}
C

In this example, we open a file named “file.txt” in write mode, write a string to it, and then close the file.

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 opening file!\n");
        return 1;
    }

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

    fclose(fptr);

    return 0;
}
C

In this example, we open a file named “file.txt” in read mode, read a string from the file, print it, and then close the file.

Wrapping Up

File modes and operations in C are essential for handling files in your programs. They offer a huge range of possibilities, from reading and writing data to appending data to an existing file. With a bit of practice, you’ll be a file handling pro in no time!

Frequently Asked Questions (FAQ)

  1. What is the mode of operation of a file?

    The mode of operation of a file determines how we can interact with the file. It’s like a set of rules that governs what we can and can’t do with a file.

  2. What are file handling operations in C?

    File handling operations in C allow us to interact with files. We can perform various operations on files, such as reading from a file, writing to a file, and closing a file.

  3. What is the difference between R+ and W+ in C?

    The ‘R+’ mode opens a file for both reading and writing. The file must already exist. The ‘W+’ mode opens a file for both reading and writing. If the file exists, its contents will be overwritten. If it does not exist, a new file will be created.

  1. Introduction to File Handling in C
  2. Reading from and Writing to Files in C
  3. Working with Binary Files in C
  4. Working with Text Files in C

Dr. Mehedi Hasan is a seasoned semiconductor professional, academic and web-designer with over a decade of experience in digital system design and verification as well as web development. Currently a Senior Engineer at AMD in Markham, Ontario, he plays a key role in the development and verification of cutting-edge chip technologies, earning multiple Spotlight Awards for his contributions.

Dr. Hasan holds a Ph.D. in Electrical and Computer Engineering from the University of Saskatchewan and has served in both academia and industry across Canada, Bangladesh, Malaysia, and Saudi Arabia. His expertise spans web-development, UVM-based SystemVerilog verification, static timing analysis (STA), RTL design, and scripting in multiple languages including Python, TCL, Shell as well as web-development tools including HTML, CSS, Javascript.

Passionate about knowledge sharing and education, Dr. Hasan has also worked as an Assistant Professor in Ontario, Canada (at Lakehead University) and Bangladesh University. He is committed to building accessible learning environments and is the founder of SkillSeminary, a platform focused on simplifying complex tech concepts for learners worldwide.

When he's not immersed in chip verification or educational projects, Dr. Hasan enjoys mentoring, researching system development, and promoting open tech education.

Scroll to Top