Examples of W Write Mode in C Programming
Introduction
In C programming, the "w" mode of a file
is used to open a file for writing. When a file is opened in "w"
mode, it will be created if it does not already exist. If the file does exist,
its contents will be truncated (i.e., all data in the file will be deleted).
What is the W write mode in C programming?
Syntax of opening a file in W write mode
The following syntax is used to open a file in W write mode in C programming language:Examples of using W write mode in C programming
The following example demonstrates how to write a string to a file using the W write mode:
#include <stdio.h>
In the above example, we have opened a file called "example.txt" in W write mode and written a string "This is an example string." to the file using the fprintf() function.
Writing an integer to a file using W write mode
Write
a C program to store "welcome to file handling" massage into data
file avn.txt
#include<stdio.h>
#include<conio.h>
main()
{
FILE*fp;
fp=fopen("d:\\avn.txt","w");
fprintf(fp,"%s","welcome
to file handling");
fclose(fp);
getch();
}
Example
2:
Write
a C program to create a data file 'avn,dat' and store student name, age and percentage
of few students.
#include<stdio.h>
#include<conio.h>
main()
{
char name[20];
int age;
float per;
char c='y';
FILE *fp;
fp=fopen("d:\\avn.dat","w");
while(c=='y')
{
printf("enter
name\n");
scanf("%s",name);
printf("Enter
age\n");
scanf("%d",&age);
printf("Enter
percentae\n");
scanf("%f",&per);
fprintf(fp,"%s%d%f",name,age,per);
printf("Do you need
more records press y");
c=getche();
}
fclose(fp);
}
Example
3:
Write a C program to create a data file student.txt and store student name and marks of 6 subjects of 5 students.
#include <stdio.h>
int main(void)
{
// Open the file for writing
FILE *fp = fopen("student.txt", "w");
if (fp == NULL)
printf("Error opening file!\n");
return 1;
}
for (int i = 0; i < 5; i++) {
char name[100];
int marks[6];
printf("Enter students name %d: ", i);
scanf("%s", name);
printf("Enter students marks %d: ", i);
for (int j = 0; j < 6; j++) {
scanf("%d", &marks[j]);
}
fprintf(fp, "%s ", name);
for (int j = 0; j < 6; j++) {
fprintf(fp, "%d ", marks[j]);
}
fprintf(fp, "\n");
}
fclose(fp);
return 0;
}
Example 4:
Write a program to create a data file emp.txt and store employee name, post and salary of 'n' employees
#include <stdio.h>
int main(void)
{
// Open the file for writing
FILE *fp = fopen("emp.txt", "w");
if (fp == NULL) {
printf("Error opening file!\n");
return 1;
}
// Read the number of employees
int n;
printf("Enter the number of employees: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
char name[100];
char post[100];
float salary;
// Read the name, post, and salary of the employee
printf("Enter the name of employee %d: ", i);
scanf("%s", name);
printf("Enter the post of employee %d: ", i);
scanf("%s", post);
printf("Enter the salary of employee %d: ", i);
scanf("%f", &salary);
// Write the name, post, and salary to the file
fprintf(fp, "%s %s %.2f\n", name, post, salary);
}
// Close the file
fclose(fp);
return 0;
No comments:
Post a Comment