471,073 Members | 1,449 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

Writing a file to /dev/lp0 in c++

MAx
Hi guys,
I am a c++ newbee and i am trying to write a file to a default
printer.
Please have a look at the code below and let me know if it'll work.

I know that similar code in C will work.
I was wondering how will the associated driver react.....

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
char buffer[50];
ifstream inFile("print_data.txt", ios::in);
ofstream outFile("/dev/lp0",ios::out);

if (!inFile){
cout << "Unable to open the file, print_data.txt"<< endl;
return 1;
}

if(!outFile){
cout << "Unable to open file, /dev/lp0" << endl;
return 1;
}

while (inFile >buffer) // Copying data from text file
outFile << buffer; // Writing data to the Device file
inFile.close();
outFile.close();
return 0;

}

Sep 14 '07 #1
2 4127
On 9/14/2007 1:40 PM, MAx wrote:
Hi guys,
I am a c++ newbee and i am trying to write a file to a default
printer.
Please have a look at the code below and let me know if it'll work.

I know that similar code in C will work.
I was wondering how will the associated driver react.....

#include <iostream>
#include <fstream>

using namespace std;

int main()
{
char buffer[50];
ifstream inFile("print_data.txt", ios::in);
ofstream outFile("/dev/lp0",ios::out);

if (!inFile){
cout << "Unable to open the file, print_data.txt"<< endl;
return 1;
}

if(!outFile){
cout << "Unable to open file, /dev/lp0" << endl;
return 1;
}

while (inFile >buffer) // Copying data from text file
outFile << buffer; // Writing data to the Device file
inFile.close();
outFile.close();
return 0;

}
I know this is OT here, but why don't you simply output using "std::cout" and
let "lp" or "lpr" write your data to the printer ? That what it's for!
S.
--
Stefan Naewe stefan dot naewe at atlas-elektronik dot com
Don't top-post http://www.catb.org/~esr/jargon/html/T/top-post.html
Plain text mails only, please http://www.expita.com/nomime.html
Sep 14 '07 #2
On Sep 14, 1:40 pm, MAx <mahesh1...@gmail.comwrote:
I am a c++ newbee and i am trying to write a file to a
default printer. Please have a look at the code below and
let me know if it'll work.
The obvious answer is that it is very system dependant, and you
should ask in a system specific newsgroup. Your filenames look
Unix; normally, under Unix, a normal user cannot write to a
printer; you have to pipe to the spooler: "lp" or "lpr",
depending on the phases of the moon. (Every other system I've
worked on has had a pseudo-device, which when opened, would act
like a pipe to the spooler. Why Unix doesn't do this is beyond
me.)
I know that similar code in C will work.
Not on any of the Unix systems I've worked on (unless you are
super-user).
I was wondering how will the associated driver react.....
If the system is configured correctly, the open will fail.

The simplest, most portable way of writing to the printer is to
write to a temporary file, then, when you close the file, use
"system()" to invoke the spooler, having obtained the correct
command from en environment variable. (Most spoolers have an
option to tell them to delete the file once they've printed it,
or to make a copy before returning, so you can delete it after
having returned from system.)
#include <iostream>
#include <fstream>
using namespace std;
int main()
{
char buffer[50];
ifstream inFile("print_data.txt", ios::in);
ofstream outFile("/dev/lp0",ios::out);
if (!inFile){
cout << "Unable to open the file, print_data.txt"<< endl;
return 1;
}
if(!outFile){
cout << "Unable to open file, /dev/lp0" << endl;
return 1;
}
while (inFile >buffer) // Copying data from text file
outFile << buffer; // Writing data to the Device file
This loop will definitely not do what you expect. It reads
white space separated words from the input (and will have
undefined behavior if the file contains a sequence of more than
49 characters without a white space), and copies them, with no
intervening white space, to the output. The easiest way to copy
an entire file is:

outFile << inFile.rdbuf() ;

Otherwise, *don't* use >and <<. With the exception of the
above, they parse and format, and in particular >strips white
space. Otherwise, You can do the copy without an explicit
buffer by means of either:

char tmp ;
while ( inFile.get( tmp ) ) {
outFile.put( tmp ) ;
}

or

std::copy( std::istreambuf_iterator< char >( inFile ),
std::istreambuf_iterator< char >(),
std::ostreambuf_iterator< char >( outFile ) ) ;
inFile.close();
outFile.close();
return 0;
}
Don't forget to check that outFile.close() succeeded, and return
EXIT_FAILURE if it hasn't (along with an error message on
std::cerr).

--
James Kanze (GABI Software) email:ja*********@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

Sep 15 '07 #3

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

48 posts views Thread by Joseph | last post: by
6 posts views Thread by Sebastian Kemi | last post: by
3 posts views Thread by ishekar | last post: by
2 posts views Thread by melanieab | last post: by
reply views Thread by Yunus's Group | last post: by
16 posts views Thread by Claudio Grondi | last post: by
6 posts views Thread by arne.muller | last post: by
3 posts views Thread by Barry Flynn | last post: by
reply views Thread by leo001 | last post: by

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.