Putw in c

After reading putw in c post you will be ready to use putw function in proper manner.

What is putw in c?

putw in c is used to write an integer value to a file. It is a file handling function in c.

It does not causes special alignment in a file.

putw() in c is an output function.

Putw in c is found in standard input/output library i.e. stdio.h.

Prototype

int putw(int w,FILE *strm);

Parameters for putw in c

The putw function writes the word w of type int to the output stream of type FILE.

Return value of putw in c

This function returns zero on successful operation otherwise non-zero value is returned.


Example programs for putw in c

Example 1

#include<stdio.h>
int main()
{
  FILE *fpointer; 
  int m;
  fpointer=fopen("scholar.txt","w");
  for(m=1;m<=20;m++)
  {
    putw(m,fpointer);
  }
  fclose(fpointer); 
  return 0;
}
putw in c example 1

Code explanation

putw is placed inside the for loop and a file pointer is passed to it. In each iteration, the value is printed onto the file which is being pointed to by file pointer.


Example 2

#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("ntEnter any number : ");
    scanf("%d",&number);
    putw(number,fp);
  }
  printf("ntData written to the filennt");
  fclose(fp);
}
putw in c example 2

Code explanation

The values taken from the user are passed to the putw function which puts these values into the file with the help of a file pointer.


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("ntEnter any number : ");
    scanf("%d",&number);
    putw(number,fp);
  }
  printf("ntData written to the file");
  fclose(fp);
  fp = fopen("scholar.txt","r");
  if(fp == NULL)
  {
    printf("ntFile doesn't exist or Can't open the file.");
    return(0);
  }
  printf("ntData read from file.n");
  while((number = getw(fp))!=EOF)     
    printf("nt%d",number);
  fclose(fp);
  printf("nn");
  return 0;
}
putw getw in c

Code explanation

In the above example, we are opening a file “scholar.txt” in write mode. If the file doesn’t exist we are printing an error message on the console.

Using for loop we are asking users to input numbers and with the help of putw function, we are putting those data to a “scholar.txt” file. After that, we are closing a file using fclose().

Next, we are opening “scholar.txt” in reading mode. until we encounter EOF we are reading an int using getw() function and put it on the console.

After completion of operation we are closing a file using fclose().

putw() Man page


Differences between putw and putchar in c

Lets look at Putw vs putchar in c programming language.

Putchar writes a single character to the console. Whereas putw is used to write an integer value into a file.

Putchar returns the int value of the character it wrote to the output console. Whereas putw returns zero on successful operation otherwise non-zero value is returned.

Putw requires a pointer to file but putchar doesnt requires a pointer.

Prototype is as below

int putw(int w,FILE *strm);

int putchar(int);
putw in cputchar in c
Used to write an integer value into a file.It writes single character to stdout.
Returns zero on successful operation otherwise non-zero value is returned.It returns the int value of the character it wrote to the console.
int putw(int w,FILE *strm);int putchar(int);

What is the difference between putw and puts in c?

puts in c function is used to write a string to stout. Whereas putw function is used to write an integer value into a file.

Putw requires a pointer to file but puts doesn’t requires a pointer.

putw returns zero on successful operation otherwise non-zero value is returned. puts returns a non-negative value on completion otherwise EOF is returned.

Prototype is as follows

int puts(const char *strng);
int putw(int w,FILE *strm);
putw in cputs in c
Used to write an integer value into a file.Used to write a string to stout.
Returns zero on successful operation otherwise non-zero value is returned.Returns a non-negative value on completion otherwise EOF is returned.
int putw(int w,FILE *strm);int puts(const char *strng);

What is the difference between putw and putc in c?

putw function is used to write an integer value into a file. putc function is used to write a character specified by a character to the specified stream.

Both are included in stdio.h header file.

putw returns zero on successful operation otherwise non-zero value is returned. putc returns character written as an unsigned char cast to an int or else EOF on error.

Prototype is as below

int putw(int w,FILE *strm);

int putc(int char,FILE *strm);
putw in cputc in c
Used to write an integer value into a file.Used to write a character specified by a character to the specified stream.
Returns zero on successful operation otherwise non-zero value is returned.Returns character written as an unsigned char cast to an int orelse EOF on error.
int putw(int w,FILE *strm);int putc(int char,FILE *strm);

What is the difference between putw and fputs in c?

putw function is used to write an integer value into a file. fputs writes a string to the specified stream.

Both uses pointer.

Both are included in stdio.h header file.

putw returns zero on successful operation otherwise non-zero value is returned. fputs returns a non-negative value on completion or else EOF on error.

Prototype is as below

int putw(int w,FILE *strm);

int fputs(const char *str,FILE strm);
putw in cfputs in c
Used to write an integer value into a file.Used to write a string to the specified stream.
Returns zero on successful operation otherwise non-zero value is returned.Returns a non-negative value on completion orelse EOF on error.
int putw(int w,FILE *strm);int fputs(const char *str,FILE strm);

What is the difference between putw and fputc in c?

putw in c is used to write an integer value into a file. fputc writes a character specified by the argument char to the specified stream.

Both are included in stdio.h header file.

putw in c returns zero on successful operation otherwise non-zero value is returned. fputc returns the character written otherwise EOF on error.

Prototype is as below

int putw(int w,FILE *strm);

int fputc(int char,FILE *strm);
putw in cfputc in c
putw function is used to write an integer value into a file.Writes a character specified by the argument char to the specified stream.
Returns zero on successful operation otherwise non-zero value is returned.Returns the character written otherwise EOF on error.
int putw(int w,FILE *strm);int fputc(int char,FILE *strm);

People also ask for

Is putw equivalent to fwrite?

If num is int then putw is equivalent to fwrite function except there is a different return value.

What is the difference between putw and getw in c?

getw is used to read integer values from a file. putw used to write an integer value to a file.

Is putw works on single character?

Putw writes an integer. An integer can be more than one digit. Simply we can say that it writes more than one byte to a file.

Is putw works on any mode of file?

putw in c works only when file opened in binary mode.

What to do if one want to write a number to a file in human readable format?

It’s better to use fprintf because it it easy by specifying format string.

Why in some cases putw is worse case?

This is because.
1.Makes your code less portable.
2.Makes your code less readable.
3.It may introduce bugs.