fdopen

Associates a stream with a file handle that represents an open file or device

Local Servers:nonblocking
Remote Servers:nonblocking
Platform:NLM
Service:Stream I/O

Syntax

  #include <stdio.h> 
    
  FILE *fdopen  (  
     int           handle,   
     const char   *mode);
  

Parameters

handle
(IN) Specifies a file handle.
mode
(IN) Points to a file mode.

Return Values

The fdopen function returns a pointer to the object controlling the stream. This pointer must be passed as a parameter to subsequent functions for performing operations on the file. If the open operation fails, fdopen returns a NULL pointer. When an error has occurred, errno is set.

Remarks

The fdopen function associates a stream with handle, which represents an opened file or device. The handle was returned by a creat or open function. mode must match the mode with which the file or device was originally opened.

If mode does not match the access flags used in opening the file originally, errno will be set to EINVAL and fdopen will fail. This includes the mode accesses read, write, append, and binary. See argument oflag for function open.

The fdopen function opens the file whose name is the string pointed to by filename, and associates a stream with it. The argument mode points to a string beginning with one of the following sequences:

r

Opens file for reading.

w

Creates file for writing, or truncates to zero length; uses default file translation.

a

Appends; opens or creates text file for writing at end-of-file; uses default file translation.

rb

Opens binary file for reading.

rt

Opens text file for reading.

wb

Creates binary file for writing, or truncates to zero length.

wt

Creates text file for writing, or truncates to zero length.

ab

Appends; opens or creates binary file for writing at end-of-file.

at

Appends; opens or creates text file for writing at end-of-file.

r+

Opens file for update (reading and/or writing); uses default file translation.

w+

Creates file for update, or truncates to zero length; uses default file translation.

a+

Appends; opens or creates file for update, writing at end-of-file; uses default file translation.

r+b

Opens binary file for update (reading and/or writing).

r+t

Opens text file for update (reading and/or writing).

w+b

Creates binary file for update, or truncates to zero length.

w+t

Creates text file for update, or truncates to zero length.

a+b

Appends; opens or creates binary file for update, writing at end-of-file.

a+t

Appends; opens or creates text file for update, writing at end-of-file.

rb+

Opens binary file for update (reading and/or writing).

rt+

Opens text file for update (reading and/or writing).

wb+

Creates binary file for update, or truncates to zero length.

wt+

Creates text file for update, or truncates to zero length.

ab+

Appends; opens or creates binary file for update, writing at end-of-file.

at+

Appends; opens or creates text file for update, writing at end-of-file.

See Also

fopen, freopen, open, sopen

fdopen Example

This example shows how to reverse the effects of redirecting stdin.

  #include <stdio.h> 
  int func (char *filepath ) 
  {  
     int     fd, stdin_fd; 
     char    line[512]; 
     FILE   *fp; 
   
     stdin_fd = fileno(stdin);   /*save descriptor for ’stdin’ */ 
     fd  = dup(stdin_fd); 
   
     if (fd == -1) 
        return -1;               /* failed to duplicate input descriptor */ 
   
     /* use the duplicated descriptor to redirect input... */ 
     fp = fdopen (fd, "r"); 
   
     if (!fp) 
        return -2;               /* failed to open duplicated descriptor */ 
   
     stdin = freopen (filepath, "r", fp); 
   
     if (!stdin) 
        return -3;               /* failed to redirect stream input */ 
   
     /* use redirected stream (example)... */ 
     while (gets(line)) 
        printf("%s\n", line); 
   
     /* UNDO: now undo the effects of redirecting input... */ 
     fclose(stdin); 
   
     stdin = fdopen(stdin_fd, "r"); 
   
     if (!stdin) 
        return -4;               /* failed to reestablish ’stdin’ */ 
   
     return 0; 
  }
  

If stdin is redirected by a console command such as

  LOAD NLM-NAME /(CLIB_OPT)/<filename>
  

you can likewise return the standard input to the keyboard by using the statements following the "UNDO" comment in the example.