can you make an ifstream from a FILE* ?

G

George

Hi All,

In trying to learn streams I'd like to rewrite this from the popen man
page:

#include <stdio.h>
#include <stdlib.h>
main( )
{
char *cmd = "/usr/bin/ls *.c";
char buf[BUFSIZ];
FILE *ptr;

if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);
(void) pclose(ptr);
return 0;
}


I was going to try to use istream_iterator and std::copy. Is it
possible to get the FILE* into an ifstream object?

Thanks much for my stupid question. Sincerely, G
 
B

BobR

George wrote in message
Hi All,

In trying to learn streams I'd like to rewrite this from the popen man
page:

<an uneducated suggestion>
Look in the iostream docs:

"Reading/writing from/to a pipe"

The procbuf class is a GNU extension. It is derived from streambuf. A procbuf
can be closed (in which case it does nothing), or open (in which case it
allows communicating through a pipe with some other program).

Constructor: procbuf::procbuf (const char *command, int mode)
Calls `procbuf::eek:pen (command, mode)'.
 
N

Neil Cerutti

Hi All,

In trying to learn streams I'd like to rewrite this from the popen man
page:

#include <stdio.h>
#include <stdlib.h>
main( )
{
char *cmd = "/usr/bin/ls *.c";
char buf[BUFSIZ];
FILE *ptr;

if ((ptr = popen(cmd, "r")) != NULL)
while (fgets(buf, BUFSIZ, ptr) != NULL)
(void) printf("%s", buf);
(void) pclose(ptr);
return 0;
}


I was going to try to use istream_iterator and std::copy. Is
it possible to get the FILE* into an ifstream object?

A quick perusal of my docs and the draft standard suggests it is
not possible with a standard function.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,777
Messages
2,569,604
Members
45,218
Latest member
JolieDenha

Latest Threads

Top