Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
Explain write mode operation of files in C language
File write mode in C is used to open a file for writing data. When a file is opened in write mode, you can store data permanently in the file, which persists even after the program terminates.
Need of Files
- Entire data is lost when a program terminates.
- Storing in a file preserves the data even if the program terminates.
- If you want to enter a large amount of data, normally it takes a lot of time to enter them all.
- We can easily access the content of files by using few commands.
- You can easily move your data from one computer to another without changes.
- By using C commands, we can access the files in different ways.
Operations on Files
The operations on files in C programming language are as follows −
- Naming the file
- Opening the file
- Reading from the file
- Writing into the file
- Closing the file
Syntax
The syntax for declaring a file pointer is as follows −
FILE *file_pointer;
The syntax for opening a file in write mode is as follows −
file_pointer = fopen("filename", "w");
Note: File operations like fopen(), fprintf(), and fclose() require actual file system access. The examples below demonstrate the syntax and logic, but won't create actual files in the online compiler environment.
Example 1: Writing Student Records
Following is the C program to read name and marks of students and store them in a file −
#include <stdio.h>
#include <stdlib.h>
int main() {
char name[50];
int marks, i, num;
printf("Enter number of students: ");
scanf("%d", &num);
FILE *fptr;
fptr = fopen("students.txt", "w"); // opening file in write mode
if(fptr == NULL) {
printf("Error! Could not open file
");
exit(1);
}
for(i = 0; i < num; ++i) {
printf("For student %d\nEnter name: ", i+1);
scanf("%s", name);
printf("Enter marks: ");
scanf("%d", &marks);
fprintf(fptr, "Name: %s, Marks: %d
", name, marks);
}
fclose(fptr);
printf("Data written to file successfully!
");
return 0;
}
Example 2: Writing and Reading Employee Details
Following is the C program for storing employee details in a file and reading them back −
#include <stdio.h>
int main() {
FILE *fp;
int eno;
char ename[30];
float sal;
// Writing to file
fp = fopen("employee.txt", "w");
if(fp == NULL) {
printf("Error opening file!
");
return 1;
}
printf("Enter employee number, name, and salary: ");
scanf("%d %s %f", &eno, ename, &sal);
fprintf(fp, "%d %s %.2f", eno, ename, sal);
fclose(fp);
// Reading from file
fp = fopen("employee.txt", "r");
if(fp == NULL) {
printf("Error opening file!
");
return 1;
}
fscanf(fp, "%d %s %f", &eno, ename, &sal);
printf("Employee Number: %d
", eno);
printf("Employee Name: %s
", ename);
printf("Salary: %.2f
", sal);
fclose(fp);
return 0;
}
Key Points
- Write mode
"w"creates a new file or overwrites existing content. - Always check if
fopen()returnsNULLto handle file opening errors. - Use
fprintf()to write formatted data to files. - Always close the file using
fclose()to ensure data is saved properly.
Conclusion
File write mode in C allows permanent data storage using fopen() with "w" mode. Always include error checking and properly close files to ensure data integrity and prevent resource leaks.
