Want to win a PS4? Go Premium and enter to win our High-Tech Treats giveaway. Enter to Win

x
?
Solved

Converting int file descriptor to (file *) struct

Posted on 2000-02-24
3
Medium Priority
?
825 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 900 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

New feature and membership benefit!

New feature! Upgrade and increase expert visibility of your issues with Priority Questions.

Question has a verified solution.

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

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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 video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

610 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