by Dinesh Thakur Category: File Handling

Function putw()

This function writes an integer (whole number) into a file. The prototype of the function is as shown below.

putw (Number, FILE* Stream);

The first argument is the number to be written and the second is the file pointer. The header file <stdio. h> should be included in the program. The following is an illustration of the code:

int n = 100;

FILE* Fptr;

putw( n, Fptr );

The function writes one value at a time. For a number of values to be written, the function may be called again with the help of a loop.

Function getw ()

This function reads an integer data from the file. The function may be called in the following manner:

FILE* Fptr;

getw (Fptr);

If a number of values are to be obtained we may use a loop to call the function several times.

Illustrates the application of functions putw () and getw ().

#include <stdio.h>

#include <stdlib.h> // for exit function
void main()
  FILE* Fptr;
  int i, j, k[6];
  Fptr = fopen("file1","w");
  if(Fptr ==NULL)
      printf("File could not be opened.");
      exit (1);
       fprintf(stdout, "File is open. Enter data.\n");
       for(i =0; i <6; i++)
       putw(k[i] = 5*i, Fptr );
       Fptr = fopen ("file1","r");
       printf("The values of elements of array k are as below.\n");
       for (j =0; j<6; j++)
       printf("%d\t", getw(Fptr));
       printf ("\n");

Function putw()and getw() in C

About Dinesh Thakur

Dinesh ThakurDinesh Thakur holds an B.C.A, MCSE, MCDBA, CCNA, CCNP, A+, SCJP certifications. Dinesh authors the hugely popular blog. Where he writes how-to guides around Computer fundamental , computer software, Computer programming, and web apps. For any type of query or something that you think is missing, please feel free to Contact us.