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

M

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::eek:ut);

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;

}
 
S

Stefan Naewe

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::eek:ut);

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.
 
?

=?ISO-8859-1?Q?Erik_Wikstr=F6m?=

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::eek:ut);

No need to specify ios::in or ios::eek:ut.
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

Don't think that is a good idea. The stream have no idea how much it
should read for each iteration of the loop. I would probably use either
read() and write(), or use std::copy() together with stream iterators
(untested):

#include <algorithm>
#include <fstream>
#include <iterator>

int main()
{
std::ifstream in("test.txt");
std::eek:fstream out("out.txt");

std::istream_iterator<char> inIt(in);
std::istream_iterator<char> endIt;
std::eek:stream_iterator<char> outIt(out);

std::copy(inIt, endIt, outIt);
}
 
J

James Kanze

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::eek:ut);
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 said:
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).
 

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,995
Messages
2,570,236
Members
46,821
Latest member
AleidaSchi

Latest Threads

Top