473,394 Members | 1,794 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

popen() and writing to returned file pointer

Hi
What is the best way to write to file pointer returned by popen().
I'm writing on Unix / c++ so I don't want to use fprintf() and then
fflush(). I found on
the net that write() can be used but I don't know excatly how to use it
with this function.
Thanks very much

Kamil Grymuza
Jul 23 '05 #1
5 3069
Kamil Grymuza wrote:

Hi
What is the best way to write to file pointer returned by popen().
I'm writing on Unix / c++ so I don't want to use fprintf() and then
fflush().
Why not? What is wrong with fprintf and fflush on Unix such that
it can't be used.
I found on
the net that write() can be used but I don't know excatly how to use it
with this function.


I guess you mean fwrite().
Since all you have is a FILE* (from popen()) you need to use the family
of functions which take a FILE* as the file descriptor.

--
Karl Heinz Buchegger
kb******@gascad.at
Jul 23 '05 #2
Karl Heinz Buchegger wrote:
Why not? What is wrong with fprintf and fflush on Unix such that
it can't be used.

fprintf() is a ANSI C function and I would like to use more 'c++'
version :) and possibly not convert string value to const char*.
So is there function (standard c++) that I could use ? Taking string and
file pointer?

I guess you mean fwrite().
Since all you have is a FILE* (from popen()) you need to use the family
of functions which take a FILE* as the file descriptor.


Yes I meant fwrite()

Thanks
Jul 23 '05 #3
Kamil Grymuza wrote:
Hi
What is the best way to write to file pointer returned by popen().
I'm writing on Unix / c++ so I don't want to use fprintf() and then
fflush(). I found on
the net that write() can be used but I don't know excatly how to use it
with this function.
Thanks very much

Kamil Grymuza


This is, of course, completely non-standard, but if you are using a
suitable platform you can construct a stream object from the FILE *.
The following program demostrates the correct (I think) way to do this.

Alan

#include <ext/stdio_filebuf.h>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <iostream>

using namespace std ;

int main()
{
FILE *fp = popen("ls -l", "r") ;
__gnu_cxx::stdio_filebuf<char> fb(fp, ios::in) ;
istream f(&fb) ;
for (char c = f.get(); !f.eof(); c = f.get())
cout << c ;

pclose(fp) ;
return 0 ;
}
Jul 23 '05 #4


Kamil Grymuza wrote:
Karl Heinz Buchegger wrote:
Why not? What is wrong with fprintf and fflush on Unix such that
it can't be used.

fprintf() is a ANSI C function


It is also an ANSI (ISO) standard C++ function.
and I would like to use more 'c++'
version :) and possibly not convert string value to const char*.
So is there function (standard c++) that I could use ? Taking string and
file pointer?


It can't be done portably. Then again, your program is already
platform-specific due the the use of the non-standard popen(). As there
is NO standard solution to your problem, this is not the right
newsgroup for you. You need to immediately ask on a newsgroup
applicable to your implementation. Possibly comp.unix.programmer will
be correct.

Brian

Jul 23 '05 #5


Alan Johnson schreef:
This is, of course, completely non-standard, but if you are using a
suitable platform you can construct a stream object from the FILE *.
The following program demostrates the correct (I think) way to do this.

Alan

#include <ext/stdio_filebuf.h>
#include <cstdlib>
#include <cstdio>
#include <unistd.h>
#include <iostream>

using namespace std ;

int main()
{
FILE *fp = popen("ls -l", "r") ;
__gnu_cxx::stdio_filebuf<char> fb(fp, ios::in) ;


__gnu_cxx::stdio_filebuf is of course platform-specific, but the
implemented
functionality isn't. It's not too hard to write a streambuf that writes
to a
FILE*. Your std::fstream streambuf might also support it directly.

Regards,
Michiel Salters

Jul 23 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Stuart McGraw | last post by:
When I run (Python 2.3.3) this script on a MS Windows 2000 machine: import os print "1st popen..." f = os.popen ("echo " + u"\u0054\u0045\u0053\u0054.txt") print f.read () print "2nd popen..."...
2
by: Your Friend | last post by:
Hello All, I'm relatively new to Python programming but have been working on this problem for a little bit now ... I initially began writing UNIX scripts in Python and thought it was the...
0
by: Kamil Grymuza | last post by:
Hi What is the best way to write to file pointer returned by popen(). I'm writing on Unix / c++ so I don't want to use printf(). I found on the net that write() can be used but I don't know...
8
by: SP | last post by:
The following code crashes after I add the two nested FOR loops at the end, I am starting to learn about pointers and would like to understand what I'm doing wrong. I think the problem is the way...
8
by: dmoore | last post by:
Hi folks, I've seen the following issue come up in multiple posts to this mailing list: I have a python program that spawns a child process with popen or popen2 or popen3 or popen2.popen2...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with...
15
by: Daniel Klein | last post by:
I'm trying to get popen to work on Windows. Here's a simplified example of what I'm trying to get working: I have a hw.c program as follows: #include <stdio.h> main() { printf ("Hello...
25
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the...
1
by: EEK | last post by:
Hello, My goal is to start and stop separate Linux processes from a python program by specific PID. The output of these processes needs to have their stderr and stdout piped to a particular...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.