Reading from standard in

  • Thread starter Olivier Maurice
  • Start date
O

Olivier Maurice

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::eek:ut);
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::eek:ut);
while ( !cin.eof() )
{
cin.read(buffer, 1000);
outputfile.write(buffer, 1000);
}
outputfile.close();
 
D

David Hilsee

Olivier Maurice said:
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?")?
 
O

Olivier Maurice

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

Karl Heinz Buchegger

Olivier said:
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: (e-mail address removed) Web: www.gascad.com

Fuer sehr grosse Werte von 2 gilt: 2 + 2 = 5
 

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

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top