
- C - Home
- C - Overview
- C - Features
- C - History
- C - Standards
- C - Environment Setup
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Basic Syntax
- C - User Input
- C - printf Function
- C - Format Specifiers
- Lexical Elements in C
- C - Tokens
- C - Keywords
- C - Identifiers
- Variables and Constants
- C - Variables
- C - Constants
- C - Const Qualifier
- C - Linkage
- Data Types and Type Conversions
- C - Data Types
- C - Literals
- C - Escape sequences
- C - Booleans
- C - Integer Promotions
- C - Character Arithmetic
- C - Type Conversion
- C - Type Casting
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Unary Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Miscellaneous Operators
- Decision Making & Control Statements
- C - Decision Making
- C - if statement
- C - if...else statement
- C - if...else if Ladder
- C - Nested if statements
- C - Switch statement
- C - Nested switch statements
- C - Switch Case Using Range
- Loops in C
- C - Loops
- C - For Loop
- C - While Loop
- C - Do...while Loop
- C - For Loop vs While Loop
- C - Nested Loop
- C - Infinite Loop
- C - Break Statement
- C - Continue Statement
- C - Goto Statement
- Functions in C
- C - Functions
- C - Function Prototype
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- C - Predefined Identifier __func__
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- C - Dynamic Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Character Arrays
- C - Special Characters
- Structures and Unions in C
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Dot (.) Operator
- C - Lookup Tables
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Function Pointers
- C - Array of Function Pointers
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Pointer to an Array
- C - Pointers vs. Multi-dimensional Arrays
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Const Pointers & Pointer to Const
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Restrict Keyword
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Initialization of Pointer Arrays
- C - Flexible Array Members in Structures
- C - Structures vs Unions
- Storage Classes and Qualifiers
- C - Storage Classes
- Memory Management in C
- C - Memory Layout
- C - Memory Management
- C - Memory Address
- C - Dynamic Array Resizing
- C - Memory Leaks
- Preprocessors in C
- C - Working of Preprocessor
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- File Handling in C
- C - File I/O (File Handling)
- C - Input & Output
- C - File Operations
- C - Formatted Output
- C - getc, getchar, getch, getche
- Constants and Literals in C
- C - Macros
- C - Header Files
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
- C Programming Resources
- C - Questions & Answers
- C - Quick Guide
- C - Cheat Sheet
- C - Useful Resources
- C - Discussion
- C Online Compiler
File Operations in C
File operations in C refer to the various tasks that can be performed on files stored in a computer's storage, such as creating, reading, writing, and closing them. These process let C program work with data that stays around after the program is done running, which is different from ordinary input/output, which only works with the console.
File Operations in C
Following are the file operations −
- Opening/Creating a file
- Writing to a file
- Reading from a file
- Closing a file
Let's briefly discuss the file operations mentioned above. We have already covered these methods in detail in the File I/O chapter. Since there are additional file operations, we will cover all of them in detail in this chapter.
Opening/Creating a File
File opening is the first step, whether it is to open an existing file or create a new one to perform operations on it. In C, the fopen() function is used to create a new file or open an existing file.
Letâs see how we can open or create a file. The syntax is given below −
FILE *fopen(const char *filename, const char *mode);
Here, filename is the name of the file to be opened, and mode defines the file's opening mode.
Here, filename is the name of the file to be opened, and mode defines the file's opening mode.
Writing to a File
Writing to a file means the process of sending data from your programâs memory to a file stored on a persistent storage device, such as a hard drive, SSD, or USB drive.
The following library functions are provided to write data in a file opened in writeable mode −
- fputc() − Writes a single character to a file.
- fputs() − Writes a string to a file.
- fprintf() − Writes a formatted string (data) to a file.
Below are the syntax to write in a file −
// Write a single character to a file int fputc(int c, FILE *fp); // Write a string to a file int fputs(const char *s, FILE *fp); // Writes a formatted string (data) to a file. int fprintf(FILE *stream, const char *format [, argument, ...])
Reading from a File
When you read from a file in C, you get data from a file on a storage device and load it into your program's memory so you can work with it.
The following library functions are provided to read data from a file that is opened in read mode −
- fgetc(): Reads a single character from a file.
- fgets(): Reads a string from a file.
- fscanf(): Reads a formatted string from a file.
Below are the syntax to reading from a file −
// Reads a single character from a file int fgetc(FILE * fp); // Reads a string from a file. int fgets(buffer, sizeof(buffer), FILE *fp); // Reads a formatted string from a file. int fscanf(FILE *stream, const char *format, ...)
Closing a File
Closing a file requires using the fclose() function after performing operations on it.
Below is the syntax to close a opened file −
int fclose(FILE *fp);
Example: File Operations in C
In this C example, we will demonstrate each file operation that we discussed above −
#include <stdio.h> int main() { FILE * file; char data[100]; // Creating/Opening a file in write mode file = fopen("example.txt", "w"); if (file == NULL) { printf("Error! Could not open file.\n"); return 1; } printf("File created/opened successfully in write mode.\n"); // Writing to the file fprintf(file, "Hello, this is a test file.\n"); fprintf(file, "We are writing data into it.\n"); // Closing after writing fclose(file); printf("Data written to the file and file closed.\n\n"); // Opening the file again in read mode file = fopen("example.txt", "r"); if (file == NULL) { printf("Error! Could not open file for reading.\n"); return 1; } printf("File opened successfully in read mode.\n"); // Reading from the file printf("Reading data from the file:\n"); while (fgets(data, sizeof(data), file) != NULL) { printf("%s", data); } // Closing the file fclose(file); printf("\n\nFile closed after reading.\n"); return 0; }
Following is the output

Additional File Operations
There are also additional file operations that can be performed on a file, such as removing, rewinding, renaming, and handling errors.
Removing a File
Removing a file means deleting a specified file from storage. For example, if you have completed all tasks and operations on a file and want to delete it from storage, you can use the remove() function to delete the file.
Below is the syntax to remove/delete a file in C
int remove(FILE *fp);
Example: Removing a File
In the following, example we demonstrate how to remove a file
#include <stdio.h> int main() { // Create a file to remove FILE *file = fopen("remove_example.txt", "w"); if (file == NULL) { printf("Error creating file.\n"); return 1; } fprintf(file, "This file will be removed.\n"); fclose(file); // Remove the file if (remove("remove_example.txt") == 0) { printf("File removed successfully.\n"); } else { perror("Error removing file"); } return 0; }
Following is the output −
File removed successfully.
Rename a File
Rename means change the name of an existing file.
Below is the syntax to rename an existing file −
rename("existing file name", "new file name");
Example: Rename a File
In this following c program, we demonstrate how we can rename the file −
#include <stdio.h> int main() { char oldName[] = "example.txt"; char newName[] = "renamed_example.txt"; // Renaming the file if (rename(oldName, newName) == 0) { printf("File renamed successfully from '%s' to '%s'.\n", oldName, newName); } else { perror("Error renaming file"); } return 0; }
Following is the output −
File renamed successfully from 'example.txt' to 'renamed_example.txt'.
Rewind a File
Rewind a file means moves the file pointer back to the beginning of the file. It is useful when you want to re-read the file form the start.
Below is the syntax to rewind a file −
int rewind(FILE *fp);
Example: Rewind a File
In this example, we demonstrate how to rewind the a pointer in file.
#include <stdio.h> int main() { FILE *file; char data[100]; // Open file in write mode and add some data file = fopen("example.txt", "w+"); if (file == NULL) { printf("Error! Could not open file.\n"); return 1; } // Writing data to the file fprintf(file, "Line 1: Hello tutorialspoint!\n"); fprintf(file, "Line 2: Welcome to C programming.\n"); // Move file pointer to beginning rewind(file); printf("Reading data after rewind:\n"); while (fgets(data, sizeof(data), file) != NULL) { printf("%s", data); } // Close the file fclose(file); return 0; }
Following is the output −

Error Handling of a File
Error handling helps in identifying file-related errors.
Example
In the following c program, we demonstrate the working of error handling of file −
#include <stdio.h> int main() { FILE *file; char data[100]; // Creating/Opening a file in write mode file = fopen("example.txt", "w"); if (file == NULL) { printf("Error! Could not open file.\n"); return 1; } return 0; }
Following is the output, if we trying running the code online −
Error! Could not open file.
Conclusion
File operations in C enable program to create, read, write, rename, rewind, and delete files on a regular basis. These procedures allow data to remain after program execution, while good error handling provides safe and dependable file management.