getw in c
getw in c programming language

If you are searching for getw in c then read this blog post completely.

What is getw in c?

getw in c is a file input function used for reading the integer from a file.

It returns next word from the input stream otherwise EOF on error.

It uses a pointer to a file to read that file.

It is found in standard input output (stdio.h) header file.

getw stands for get a word from a stream.

Prototype

#include<stdio.h>
int getw(FILE * strm);

Parameters for getw in c programming language

Here FILE * is file pointer which points to a file which identifies a stream.

Return value of getw in c programming language

getw returns integer read from the file stream or EOF on error.

Example programs for getw in c

Example 1

#include<stdio.h>

int main()
{
  FILE *fpointer;
  int i;

  fpointer=fopen("scholar.txt","r");
  while((i=getw(fpointer))!=EOF)
   printf("\t%d",i);

  fclose(fpointer);
   return 0;
}

Code explanation

In above example we are opening “scholar .txt” in read mode using fopen() function.

Until we encounter end of file (EOF) we are reading an int using getw and printing it on to the console.

At the end close the file using fclose().

Example 2

#include<stdio.h>
int main()
{
  int number;
  FILE *fp = fopen("scholar.txt","r");
  if(fp == NULL)
  {
    printf("\n\tFile doesn't exist or Can't open the file.");
    return(0);
  }
  printf("\n\tData read from file.\n");
  while((number = getw(fp))!=EOF)     
    printf("\n\t%d",number);
  fclose(fp);
  printf("\n\n");
  return 0;
}
getw in c

Code explanation

The integer value from a file is read by calling getw function. The result is being printed on to the console.


Example 3

#include<stdio.h>
int main()
{
  FILE *fp;
  int number;
  fp = fopen("scholar.txt","w");
  if(fp == NULL)
  {
    printf("\nFile doesn't exist or Can't open the file.");
    return(0);
  }
  for( int i=0 ; i<3 ; i++ )
  {
    printf("\n\tEnter any number : ");
    scanf("%d",&number);
    putw(number,fp);
  }
  printf("\n\tData written to the file");
  fclose(fp);
  fp = fopen("scholar.txt","r");
  if(fp == NULL)
  {
    printf("\n\tFile doesn't exist or Can't open the file.");
    return(0);
  }
  printf("\n\tData read from file.\n");
  while((number = getw(fp))!=EOF)     
    printf("\n\t%d",number);
  fclose(fp);
  printf("\n\n");
  return 0;
}
putw getw in c
putw getw in c

Code explanation

The integer value from a file is read by calling getw function. The result is being printed on to the console.

getw() Man page


Differences between getw and putw in c?

Now lets look at getw vs putw in c programming language.

getw reads integer data from a file. Whereas putw writes integer data to a file.

getw returns next word from the input stream otherwise EOF on error. putw returns zero on successful operation otherwise non-zero value is returned.

getw is input function. putw is output function.

getw takes single argument. putw takes two arguments.

Prototypes are as below

int getw(FILE * strm);

int putw(int w,FILE *strm);
getw in cputw in c
Reads integer data from a file.writes integer data to a file.
Returns next word from the input stream otherwise EOF on error.returns zero on successful operation otherwise non-zero value is returned.
Input function.Output function.
Takes single argument.Takes two arguments.
int getw(FILE * strm);int putw(int w,FILE *strm);

Difference between getw and getc in c.

Now lets look at getw vs getc in c programming language.

In c language getw reads integer data from a file. Whereas getc reads the character from a file.

getw function returns next word from the input stream otherwise EOF on error. Whereas getc returns character read as integer data and EOF on error.

getw function reads binary data. getc reads character data.

Prototypes are as below

int getw(FILE * strm);

int getc(FILE *strm);

getw in cgetc in c
Reads integer data from a file.Reads the character from a file.
int getw(FILE * strm);int getc(FILE *strm);
Returns next word from the input stream otherwise EOF on error.Returns character read as integer data and EOF on error.
Reads binary data.Reads character data.

Difference between getw and scanf in c?

In c language scanf() function is used to take input from the user. Whereas getw reads integer data from a file.

The scanf() returns the number of input data that are scanned otherwise returns EOF if an error occurs. getw function returns next word from the input stream otherwise EOF on error.

The scanf() function reads the formatted data from standard input such as a keyboard. getw reads integer data from a file.

The scanf functions prompts for user input. getw reads data from a file.

Prototypes are as below

int getw(FILE * strm);

int scanf(const char *frmt,...);
getw in cscanf in c
Reads integer data from a file.Used to take input from the user
int getw(FILE * strm);int scanf(const char *frmt,…);
Returns next word from the input stream otherwise EOF on error.Returns the number of input data that are scanned otherwise returns EOF if error occurs.
Reads integer data from a file.Reads the formatted data from standard input such as keyboard
Reads data from a filePrompts for user input.

Difference between getw and gets in c?

gets in c programming reads characters from the standard input and stores it as a string until EOF is reached. getw reads integer data from a file.

gets returns string entered by the user. getw returns next word from the input stream otherwise EOF on error.

gets enables user to enter data. getw reads int data from a file.

gets suffers from buffer overflow as it keeps reading input until a newline is found. getw doesn’t suffer from buffer overflow.

Prototypes are as below

int getw(FILE * strm);

char *gets(char *strng);
getw in cgets in c
Reads integer data from a file.Reads characters from the standard input and stores it as a string until EOF is reached.
int getw(FILE * strm);char *gets(char *strng);
Returns next word from the input stream otherwise EOF on error.Returns string entered by the user.
Reads int data from a file.Enables user to enter data.
Doesn’t suffers from buffer overflow.Suffers from buffer overflow

Difference between getchar and getw in c.

getchar in c reads single character from the standard input stream. getw reads integer data from a file.

getchar prompts for user input. getw reads int data from a file.

getchar returns the character as unsigned char and cast to an int otherwise EOF on error. getw returns next word from the input stream otherwise EOF on error.

getchar does not takes any argument. getw takes one argument.

Prototypes are as below

int getw(FILE * strm);

int getchar(void);
getw in cgetchar in c
Reads integer data from a file.Reads single character from the standard input stream.
int getw(FILE * strm);int getchar(void);
Reads int data from a file.Prompts for user input
Returns next word from the input stream otherwise EOF on error.Returns the character as unsigned char and cast to a int otherwise EOF on error.
Takes one argument.Does not takes any argument.

People also ask for

What does getw stands for?

It stands for get a word from a stream. It reads integer data from a file.

Is getw reads character in a file?

Its purpose is only to read integer data. For reading, a character from a file one can use getc function.

What is the difference between getint and getw in c?

getint function reads digits from the input. getw reads integer word in a file.

Does getw reads floating data?

No, it only reads integer data from a file. for reading floating data one can use get float function.

What is the difference between fscanf and getw in c?

fscanf function reads formatted input from a file. Where as getw function reads only integer data from a file.