LinuxQuestions.org
Latest LQ Deal: Latest LQ Deals
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 02-16-2009, 08:16 PM   #1
akm3
LQ Newbie
 
Registered: Dec 2008
Location: Atlanta GA
Distribution: Ubuntu 8.10
Posts: 11

Rep: Reputation: 0
Opening a File by using a named pipe


Hi,

I am trying to use a function from ffmpeg library(A linux based decoder-encoder)

This function has an input like "const char *filename"

I am trying to call it with a buffer(unsigned char *buff)
So, I created a pipe, and wrote my buffer to it:

Code:
int my_pipe[2];
if(pipe(my_pipe) == -1)
{    perror("pipe call error");
        return -1;
}

write(my_pipe[1],buff,buff_size);
now, is there a way that instead of "filename", I pass my pipe to this function to read from?

(An equivalent question can be, how to call `fopen` using a file descriptor?)
 
Old 02-17-2009, 05:04 AM   #2
SciYro
Senior Member
 
Registered: Oct 2003
Location: hopefully not here
Distribution: Gentoo
Posts: 2,038

Rep: Reputation: 51
I dont know if this is portable to other OSs, but it works in Linux.

Each process has its own directory, placed in /proc and named after its process ID (thus all the directories with numbers). Each process-dir has a directory named 'fd', which contains a list of all active file descriptors. /proc/self is a shortcut to the current process-dir (so it wont work to pass such a name between multiple processes, you would have to construct the actual name with the process ID).

Simple make a filename using the above and the file descriptor, example:

Code:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main (argc, argv)
  int argc;
  char** argv;
{
    char* buf;
    buf = "apple\n";

    int my_pipe[2];
    if(pipe(my_pipe) == -1)
      {
        perror("pipe call error");
        return -1;
      }

    write(my_pipe[1], buf, strlen(buf));

    FILE* file;
    char* filename;
    asprintf (&filename, "/proc/self/fd/%i", my_pipe[1]);
    file = fopen (filename, "r");
    if (file == NULL)
      {
        perror ("unable to open file");
        return -1;
      }
    free (filename);

    char* readbuf;
    int readsize;
    readbuf = NULL;
    readsize = 0;

    getline (&readbuf, &readsize, file);
    printf ("%s", readbuf);
    free (readbuf);

    fclose (file);
    close (my_pipe[0]);

    return 0;
}
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
syslog not opening named pipe. fluxrad Linux - Software 5 02-10-2009 05:31 AM
What is a named pipe ronzoid Linux - General 1 01-18-2006 04:30 PM
unlinking named pipe sahel Programming 1 01-09-2006 10:36 PM
named pipe sahel Programming 1 01-09-2006 08:29 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 01:55 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration