Solved

Converting int file descriptor to (file *) struct

Posted on 2000-02-24
3
799 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
Comment Utility

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
Comment Utility
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
Comment Utility

Thanks

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

0

Featured Post

Highfive + Dolby Voice = No More Audio Complaints!

Poor audio quality is one of the top reasons people don’t use video conferencing. Get the crispest, clearest audio powered by Dolby Voice in every meeting. Highfive and Dolby Voice deliver the best video conferencing and audio experience for every meeting and every room.

Join & Write a Comment

This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.
In this tutorial you'll learn about bandwidth monitoring with flows and packet sniffing with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're interested in additional methods for monitoring bandwidt…

771 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now