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

What is getw() function in C?

The file is a collection of data. File contains information which might be set of character or may contain numbers.
The file also contains integer value so to deal with integer type of data getw function is used.
Like fgets() or fgetc() which are used to read character or array of character from the file, getw() function in C used to get integers or numbers from the file.
getw() function :- This function is used to read the integer value from the file.

getw() function in C
getw() function in C

Syntax for getw() function in C
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

#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: –
In the above program, we are reading integer data from a file.

1. First we open file technical.txt.
Assume technical.txt exists in location with content (content :- 10 20 30).
2. fopen file opens the successful as it exists (otherwise print can’t open file)
3. Next, we read integer data from a file.
4. getw function reads the integer data from a file and stores it to variable data.
5. We get integer value till the end of the file.EOF check whether the file reaches the end.

Output : – (Assume file technical.txt with content:- 10 20 30)
getw() function in C

Write A Comment