473,327 Members | 1,979 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,327 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 4379
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

48
by: Joseph | last post by:
Hi I'm writing a commercial program which must be reliable. It has to do some basic reading and writing to and from files on the hard disk, and also to a floppy. I have foreseen a potential...
6
by: Sebastian Kemi | last post by:
How should a write a class to a file? Would this example work: object *myobject = 0; tfile.write(reinterpret_cast<char *>(myobject), sizeof(*object)); / sebek
3
by: ishekar | last post by:
Hi, I have an application where i want to write data to a file, the data is being sent from an external source. I know the total size of the data and then i retrieve the data in small segments...
1
by: Daniel | last post by:
System.IO.StreamWriter Close or Flush method to shut down the computer in such a way that just part of the file is written? or an empty file is written? Also if the Close or Flush is to a...
2
by: melanieab | last post by:
Hi, I'm trying to store all of my data into one file (there're about 140 things to keep track of). I have no problem reading a specific string from the array file, but I wasn't sure how to...
4
by: HNguyen | last post by:
Hi, I have a Web application in ASP.NET. My Application allows the users upload files into the server after checking their user names and passwords. For each transaction, the Web program will...
0
by: Yunus's Group | last post by:
Yunus's Group May 23, 3:36 pm show options Newsgroups: microsoft.public.dotnet.languages.vb From: "Yunus's Group" <yunusasm...@gmail.com> - Find messages by this author Date: 23 May 2005...
16
by: Claudio Grondi | last post by:
I have a 250 Gbyte file (occupies the whole hard drive space) and want to change only eight bytes in this file at a given offset of appr. 200 Gbyte (all other data in that file should remain...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
3
by: Barry Flynn | last post by:
Hi I am working with a VB 2005 program which has been converted from VB6. It writes data out to a flat file, with code like the following line WriteLine(riFileNo, "Hist", lsAssetID,...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you

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.