Latest web development tutorials

C library functions - fputc ()

C standard library - <stdio.h> C standard library - <stdio.h>

description

C library functionsint fputc (int char, FILE * stream) The argument charspecified character (an unsigned character) is written to the specified stream in the stream, and the location identifier move forward.

statement

Here is () statement fputc function.

int fputc(int char, FILE *stream)

parameter

  • char - which it is to be written character.The character of its corresponding int value passed.
  • stream - This is a pointer to FILE pointer to an object, the object is FILE identifies the character to be written stream.

return value

If no error occurs, the written character is returned. If an error occurs, it returns EOF, and the error identifier.

Examples

The following example demonstrates the fputc () function is used.

#include <stdio.h>

int main ()
{
   FILE *fp;
   int ch;

   fp = fopen("file.txt", "w+");
   for( ch = 33 ; ch <= 100; ch++ )
   {
      fputc(ch, fp);
   }
   fclose(fp);

   return(0);
}

Let's compile and run the above program, which will create a filefile.txt in the current directory, it reads as follows:

!"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_`abcd

Now let's use the following procedure to view the contents of the above file:

#include <stdio.h>

int main ()
{
   FILE *fp;
   int c;

   fp = fopen("file.txt","r");
   while(1)
   {
      c = fgetc(fp);
      if( feof(fp) )
      {
          break ;
      }
      printf("%c", c);
   }
   fclose(fp);
   return(0);
}

C standard library - <stdio.h> C standard library - <stdio.h>