> getw <STDIO.H> ~ Online tutorial

getw




 getw                    <STDIO.H>

 getw gets an integer from stream



 Declaration:


   int getw(FILE *stream);



 Remarks:


 getw returns the next integer in the named input stream. It assumes no
special alignment in the file. getw should not be used when the stream is
opened in text mode.

 Return Value:
 On success,
      getw returns the next integer on the input stream.
  .
 On error,
      getw 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: