Loading....
Coupon Accepted Successfully!

 

Sending Output To Devices

It is also possible to send information of files directly to devices such as a printer or monitor. Table describes various devices along with their names and descriptions. The following program illustrates the use of such devices in the program:

Table: Standard Devices

 

Device Name

Description

CON

Console (monitor screen)

COM1 or AUX

Serial port – I

COM2

Serial port – II

LPT1 OR PRN

Parallel printer – I

LPT2

Parallel printer – II

LPT3

Parallel printer – III

NUL

Dummy device

 

16.25 Write a program to read a file and sent data to the printer.

#include<fstream.h>

#include<iostream.h>

#include<conio.h>
#include<process.h>
#define eject out.put(‘\x0C’);
void main()
{

clrscr();
char h;
char name[20];
cout<<“Enter file name:”;
cin>> name;
ifstream in (name);
if (!in)
{
cerr <<endl<<“File opening error”;
_cexit();
}
ofstream out (“LPT1”);
if(!out)
{
cerr <<endl<<“device opening error”;
_cexit();
}
while (in.get(h)!=0)
out.put(h);
eject;

}


Explanation:
In the above program, the file name is entered through the keyboard, and it is opened for reading purpose. The ifstream object in opens the file. The ofstream object out activates the printer. The if statements check both the objects for detecting operation status, that is, whether the operations have failed or are successful. The while loop reads data from the file and using the put() statement, it passes it to the devices associated with the object out. In this program, the data read are passed to the printer. The macro eject defined at the beginning of the program advances the page of the printer. In case the printer is not attached, the message displayed will be as given below.
 

Error Message

System Message

Error accessing LPT1 device

» Retry « Cancel

 
The user can select retry if he or she had attached the printer; otherwise, by selecting cancel, the operation can be cancelled.




Test Your Skills Now!
Take a Quiz now
Reviewer Name