> putw<STDIO.H> ~ Online tutorial

putw




 putw            <STDIO.H>

  putw outputs an integer on a stream

 Declaration:
 
 int putw(int w, FILE *stream);

 Remarks:

 putw outputs the integer w to the given stream. It does not expect (and
does not cause) special alignment in the file.

 Return Value:


   On success,
    putw returns the integer w.
  On error,
      putw returns EOF
   On end-of-file, getw returns EOF

Because EOF is a legitimate value for getw to return, use feof to detect
end-of-file or ferror to detect error.

Because EOF is a legitimate integer, use ferror to detect errors with putw.

Program

#include <stdio.h>
#include <stdlib.h>

#define FNAME "test.$$$"

int main(void)
{
   FILE *fp;
   int word;

   /* place the word in a file */
   fp = fopen(FNAME, "wb");
   if (fp == NULL)
   {
      printf("Error opening file %s\n", FNAME);
      exit(1);
   }

   word = 94;
   putw(word,fp);
   if (ferror(fp))
       printf("Error writing to file\n");
   else
       printf("Successful write\n");
   fclose(fp);

   /* reopen the file */
   fp = fopen(FNAME, "rb");
   if (fp == NULL)
   {
      printf("Error opening file %s\n", FNAME);
      exit(1);
   }

   /* extract the word */
   word = getw(fp);
   if (ferror(fp))
       printf("Error reading file\n");
   else
       printf("Successful read: word = %d\n", word);

   /* clean up */
   fclose(fp);
   unlink(FNAME);

   return 0;
}

Please Give Us Your 1 Minute In Sharing This Post!
Please Give Us Your 1 Minute In Sharing This Post!
SOCIALIZE IT →
FOLLOW US →
SHARE IT →
Powered By: BloggerYard.Com

0 comments: