setmode

From RAD Studio
Jump to: navigation, search

Go Up to io.h Index


Header File

io.h

Category

Input/output Routines

Prototype

int setmode(int handle, int amode);

Description

Sets mode of an open file.

setmode sets the mode of the open file associated with handle to either binary or text. The argument amode must have a value of either O_BINARY or O_TEXT, never both. (These symbolic constants are defined in fcntl.h.)

Return Value

setmode returns the previous translation mode if successful. On error it returns -1 and sets the global variable errno to

EINVAL

Invalid argument

EBADF

Bad file number


Example

#include <fcntl.h>
#include <io.h>
#include <stdio.h>
int main (int argc, char ** argv )
(
  FILE *fp;
  int newmode;
  long where;
  char buf[256];
  fp = fopen( argv[1], "r+" );
  if ( !fp )
  {
    printf( "Couldn't open %s\n", argv[1] );
    return -1;
  }
  newmode = setmode( fileno( fp ), O_BINARY );
  if ( newmode == -1 )
  {
    printf( "Couldn't set mode of %s\n", argv[1] );
    return -2
  }
  fp->flags |= _F_BIN;
  where = ftell( fp );
  printf ("file position: %d\n", where );
  fread( buf, 1, 1, fp );
  where = ftell ( fp );
  printf( "read %c, file position: %ld\n", *buf, where );
  fclose ( fp );
  return 0;
}

Portability

POSIX Win32 ANSI C ANSI C++

+