putw() function in C is the another file handling function available in c.

What is putw() function in C?

putw() function in c programming:- This function is used to write the integer value to file.
The file is the collection of data as we have getw() to read integer value we have putw() function to write integer value to file.
To read integer data from the file we use getw() function whereas putw() function used for writing integer data to file.
getw function:- This function is used to read the integer value from the file.

putw() function in C
putw() function in C

Syntax for using putw function:-
putw(int data,FILE *fp);

In the above syntax for putw function, we can see putw function accepts two parameters first is integer data to write to file and second which is file pointer which points to the file.
getw function:- This function is used to read the integer value from the file.

Example :-
int data =10;
putw(data,fp);

Where fp is file pointer and data is the integer data which you want to store in a file.
Now let’s see the program for putw in c programming.

Program to demonstrate putw() function in C

#include<stdio.h>
#include<conio.h>
void main ()
{
int data;
data = 10;
FILE *fp;
printf ("\n opening file in write mode to write integer data:");
fp=fopen ("technical.txt","w"); //opening file in write mode
if(fp == NULL)
{
printf ("\n can't open file");
exit(0);
}
printf("\nWrite Integer data to file:");
putw(data,fp); //writing data.
printf("\nData written to file successfully.");
fclose(fp);
}

Output :-. (Assume file technical.txt)

putw() function in C
Program description:-
In the above program, we are writing integer data to file.
1. First we open file technical.txt.
Assume technical.txt exists in the current directory location.
2. fopen file opens the successful as it exists (otherwise fopen creates a file with name technical.txt and opens the file)
3. Next, we store integer data to variable data which we want to store to file.
Write putw function write the integer data to file with two-parameter first is data and second is file pointer.
4. We written data now close the File. ( This is most important)
5. We can see the file which contains the data which we stored.
6. End with the program

Write A Comment