473,401 Members | 2,127 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,401 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 3071
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
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.