_putw

De RAD Studio
Aller à : navigation, rechercher

Remonter à Stdio.h - Index


Header File

stdio.h

Category

Input/output Routines

Prototype

int _putw(int w, FILE *stream);

Description

Writes an integer on a stream.

_putw outputs the integer w to the given stream. _putw neither expects nor causes special alignment in the file.

Return Value

On success, _putw returns the integer w. On error, _putw returns EOF. Because EOF is a legitimate integer, use ferror to detect errors with _putw.

Example



 #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;
 }



Portability



POSIX Win32 ANSI C ANSI C++

+