Solved

Converting int file descriptor to (file *) struct

Posted on 2000-02-24
3
814 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

696 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