By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,206 Members | 1,020 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,206 IT Pros & Developers. It's quick & easy.

Reading from standard in

P: n/a
Hi all,
I suppose some of you know the program Redmon (type redmon in google, first
result). This neat little tool allows to hook up any functionality to a
printer by putting the file printed to the printer to standard in.

You have to provide yourself the program that does something with that data,
so I thought with some quick c/c++ programming that would be no problem. I
tried dozens of ways of reading the data from stdin or cin, char by char
(like sample below), buffers... but I only got it working for text files,
e.g. pdf-files, html-files,...
When a gif or any other binary file is sent to it, the none of the ways
would work: in case of byte by byte reading it stops way too early (example
1), in case of buffered reading (example 2), it only goes through the buffer
1 time, like if he already encountered eof.

I know in case of binary files this is perfectly possible, but how to read
then binary data from the standard input?

A hint, url, some names of methods to be used, are fine. I am no noobie...
Regards,

Olivier

//example 1
ofstream logfile(fileName.c_str(), ios::binary | ios::out);
char c;
cin.get(c);
while (!cin.eof())
{
logfile << c;
cin.get(c);
}
logfile.close();

//example2
char *buffer = new char[1000];
ofstream outputfile(filenameTmp->c_str(), ios::out);
while ( !cin.eof() )
{
cin.read(buffer, 1000);
outputfile.write(buffer, 1000);
}
outputfile.close();
Jul 22 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
"Olivier Maurice" <ol*************@skynet.be> wrote in message
news:41***********************@news.skynet.be...
Hi all,
I suppose some of you know the program Redmon (type redmon in google, first result). This neat little tool allows to hook up any functionality to a
printer by putting the file printed to the printer to standard in.

You have to provide yourself the program that does something with that data, so I thought with some quick c/c++ programming that would be no problem. I
tried dozens of ways of reading the data from stdin or cin, char by char
(like sample below), buffers... but I only got it working for text files,
e.g. pdf-files, html-files,...
When a gif or any other binary file is sent to it, the none of the ways
would work: in case of byte by byte reading it stops way too early (example 1), in case of buffered reading (example 2), it only goes through the buffer 1 time, like if he already encountered eof.

I know in case of binary files this is perfectly possible, but how to read
then binary data from the standard input?

A hint, url, some names of methods to be used, are fine. I am no noobie...


Have you consulted the FAQ (http://www.parashift.com/c++-faq-lite/) setion
15 ("Input/output via <iostream> and <cstdio>") question 13 ("How can I
"reopen" std::cin and std::cout in binary mode?")?

--
David Hilsee
Jul 22 '05 #2

P: n/a
I am afraid I will need a little more hint or help :-). This is some great
resource though. Thanks already.

Have you consulted the FAQ (http://www.parashift.com/c++-faq-lite/) setion
15 ("Input/output via <iostream> and <cstdio>") question 13 ("How can I
"reopen" std::cin and std::cout in binary mode?")?

--
David Hilsee

Jul 22 '05 #3

P: n/a
Olivier Maurice wrote:

I am afraid I will need a little more hint or help :-). This is some great
resource though. Thanks already.


OK.
The FAQ clearly says:
There is no standard way to do it. It all depends on your compiler/platform.

Thus: You need to ask in a newsgroup which discusses your compiler and/or platform.
comp.lang.c++ deals with platform independent ascpects of C++ only.
--
Karl Heinz Buchegger, GASCAD GmbH
Teichstrasse 2
A-4595 Waldneukirchen
Tel ++43/7258/7545-0 Fax ++43/7258/7545-99
email: kb******@gascad.at Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
Jul 22 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.