The getw() functions in C programming

Getw Function

In this tutorial we will see what is mean by getw function, how it is defined and used in file handling function in c.

getw is the another file handling function available in c.

As file contains information , it also contains integer value so to deal with integer type of data getw function is used.

getw function :- This function is used to read the integer value from file.

getw function

getw() Syntax 

int int_variable=getw(FILE *fp);

In above syntax for getw() function, we can see getw() function accepts only one parameter which is file pointer which points to the file.and getw() function returns the integer value from file which fp pointing and it returns end of file if file reaches to end of file.

Example :-

int data=getw(fp);

   Where fp is file pointer.

Program to demonstrate getw() function in c

123456789101112131415161718192021222324252627282930313233343536373839#include<stdio.h>#include<conio.h>void main (){ int data; FILE *fp; printf (“\n opening file in read mode to read integer data:”); fp=fopen (“technical.txt”,”r”);. //opening file in read mode if(fp == NULL) { printf (“\n can’t open file”); exit(0); } printf(“\n Integer data from file:”); while((data=getw(fp))!=EOF) //read integer data { printf(“%d\n”, data); }fclose(fp);}

Program Description

  1. In above program we are reading integer data from file.
  2. First we open file technical.txt .
  3. Assume technical.txt exists in location with content (content :- 10 20 30).
  4. fopen file opens the successfully as it is exist (otherwise is prints can’t open file)
  5. Next we read integer data from file.
  6. getw function reads the integer data from file and store it to variable data .
  7. We get integer value till end of file.EOF check whether file reaches end.

Output :-. (Assume file technical.txt with content :- 10 20 30)

opening file in read mode to read integer data:

Integer data from file :

10

20

30

Filed Under: C Tagged With: getw()

Leave a Comment

Your email address will not be published.