Computer for SEE and NEB

It is a complete SEE and NEB solution for computer science. It includes Computer Fundamentals, Database (SQL), Programming in C QBASIC, CSS, JavaScript, and PHP for beginners.

Breaking

Post Top Ad

Your Ad Spot

Monday, January 9, 2023

Examples of W Write Mode in C Programming

Examples of W Write Mode in C Programming


Examples of W Write Mode in C Programming


Introduction


File handling is an essential aspect of programming, and C programming language provides an extensive set of file handling functions and modes. The W write mode is one of the modes that C programming language offers, and it allows users to create a new file or modify an existing file by writing data to it. In this article, we will discuss the Examples of W write mode in C programming.

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?


The W write mode is a file mode in C programming language that allows users to open a file for writing. If the file does not exist, the W write mode creates a new file with the specified name. If the file already exists, the W write mode overwrites the existing file's content.



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:

FILE *fp;
fp = fopen("filename", "w");


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> 
int main() 
FILE *fp; 
fp = fopen("example.txt", "w"); 
fprintf(fp, "This is an example string."); 
fclose(fp); 
return 0; 
}


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


The following example demonstrates how to write an integer to a file using the W write mode:

#include <stdio.h>
int main() {
   FILE *fp;
   fp = fopen("example.txt", "w");
   int num = 10;
   fprintf(fp, "%d", num);
   fclose(fp);
   return 0;
}



Example 1:

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; 
}

Conclusion: 


In conclusion, the W write mode is a critical mode for file handling in C programming. It allows programmers to erase the existing contents of a file and write new data to it. In this article, we have discussed the basics of file handling in C and provided examples of how to use the W write mode. By understanding the W write mode and its usage, programmers can make their file handling operations more efficient and effective.


You can also Read:



FAQs:


1) What is the difference between W write mode and A append mode in C programming? 
Ans: The W write mode erases the existing contents of a file and writes new data to it, while the A append mode appends new data to the existing data in a file.


2) Can we open a non-existent file in W write mode in C programming? 
Ans: Yes, if you try to open a file in W write mode that does not exist, a new file will be created with the specified name.


3) Is it possible to read data from a file in W write mode in C programming? 
Ans: No, the W write mode is used for writing data to a file. If you want to read data from a file, you need to use a different mode such as R read mode.


4) What happens if an error occurs while writing data to a file in W write mode in C programming? 
Ans: If an error occurs while writing data to a file in W write mode, the fprintf() function will return a negative value, and you can check for errors using the ferror() function.


5) What are some advantages of using W write mode in C programming? 
Ans: Some advantages of using W write mode include the ability to erase the existing contents of a file and start fresh, the ability to create a new file if it does not exist, and the ability to write data quickly and efficiently.

No comments:

Post a Comment

Post Top Ad

Your Ad Spot

Pages