Solved

Converting int file descriptor to (file *) struct

Posted on 2000-02-24
3
809 Views
Last Modified: 2013-12-26
Hi,

I'm really confused as to the mapping of integer file dscriptors, such as 0,1,2 (stdout and stderr) to (file *) structure on UNIX (specifically AIX), does anyone know how this is done?

Cheers

Andy
0
Comment
Question by:Ghostrider
  • 2
3 Comments
 
LVL 2

Accepted Solution

by:
rdelfino earned 300 total points
ID: 2555433

You gotta use fdopen() to map a FILE* to an integer file descriptor.

try the example below:

--------------------------8<-------------------------

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

#include <errno.h>
#include <string.h>

#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>

int main(void)
{
  FILE *fd;

  int f;
 
  f = open("/tmp/test.txt", O_CREAT | O_WRONLY, 0644);
  if (f < 0)
  {
    fprintf(stderr, "(%d - %s) error opening /tmp/test.txt\n", \
      errno, \
      strerror(errno));
    return 1;
  }
 
  fd = fdopen(f, "w");
  if (!fd)
  {
    fprintf(stderr, "(%d - %s) error opening stream\n", \
      errno, \
      strerror(errno));
    return 2;
 
  }
 
  fprintf(fd, "Hello\n");
 
  fclose(fd);
 
  close(f);
  return 0;
}
0
 

Author Comment

by:Ghostrider
ID: 2555604
kewl,

I actually wanted to map from the file struct to the integer index, but I've dealt with it, and at the expense of efficiency my code'll do!

I must remember fdopen!

Many thanks cheers

Andy
0
 
LVL 2

Expert Comment

by:rdelfino
ID: 2555627

Thanks

If you want to map a FILE* to an integer file descriptor
you gotta use fileno()

0

Featured Post

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
iSeries FTP Exit Program 8 163
Doc'in system (example?) BA 7 120
only14 challenge 19 84
withoutTen challenge 14 137
Introduction: Dialogs (1) modal - maintaining the database. Continuing from the ninth article about sudoku.   You might have heard of modal and modeless dialogs.  Here with this Sudoku application will we use one of each type: a modal dialog …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question