473,799 Members | 3,137 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_d ata.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 4413
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_d ata.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...@gma il.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_d ata.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 objektorientier ter Datenverarbeitu ng
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
8514
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 problem. The program may crash unexpectedly while writing to the file. If so, my program should detect this during startup, and then (during startup) probably delete the data added to the file and redo the writing operation.
6
23608
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
3408
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 from the source. This data is written to file in a loop. My question. 1. Will it be useful to increase the file size initially then seek to 0 and start writing to file. whether there will be any performance improvements
1
717
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 streamwriter writing to a network share, is it possible for the network to go down in such a way that the tartet file is only partialy written? or are there some kind of check sums to prevent this.
2
6861
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 replace just one item. I know I can get the entire array, then save the whole thing (with a for loop and if statements so that the changed data will be saved), but it seems like a lot of unnecessary reading and writing. Is there a way to directly save...
4
2181
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 write the information about user name, filename upload, filesize, date and time of uploading into the log file. (The name of the log file is constructed by Current Year and Current Month in my program). Is there any problems with writing into the...
0
1719
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 12:36:14 -0700 Local: Mon,May 23 2005 3:36 pm Subject: Writing to text file Reply | Reply to Author | Forward | Print | Individual Message | Show original | Remove | Report Abuse
16
7191
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 unchanged). How can I do that in Python? Claudio Grondi
6
5276
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
2699
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, lsRecordType, lsXNbr, lsFiscYr, "Beg", CStr(H.BegBalAccDepn), CStr(H.BegBalCost), CStr(H.BegBalCostReval), CStr(H.BegBalDepCost), CStr(H.BegBalDepnReval)) The program is running from within a Virtual PC
0
9688
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10260
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10243
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9078
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7570
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5467
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
4146
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3762
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2941
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.