Click here to Skip to main content
15,887,812 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
I want to print a text file at a particular location through an inkjet printer.
I have developed a code for this but it produces an error that the printer is unable to open.

The code is given below:

C++
#include <iostream.h>
#include <fstream.h>
#include <conio.h>

void main(void)
{
    clrscr();
    char filename[13];
    char ch;
    cout<<"\nEnter the text file name:";
    cin.getline(filename,13);
    cout<<"\n";
    ifstream fin(filename, ios::in);
    if(!fin)
    {
        cerr<<"\n Cannot open file !\n";
        getch();
        return;
    }
    char const * const PrinterName = "usb001:"; // Identify the printer port.
    ofstream printer(PrinterName); // Open the printer stream.
    if(!printer) // Ensure the printer stream opened ok.
    {
        cerr << "\a\n\tERROR: Unable to open " << PrinterName << endl;
        cout << "\n\tPress the [ENTER] key to return ";
        getch();
        return;
    }

    while (! fin.eof())
    {
        fin.get(ch);
        printer << (ch);
    }

    printer << endl << ends << flush;
    printer.close(); // Finish the print job by closing the printer stream.
    fin.close();

    // Send a carriage return and a formfeed to the printer so that: a) this
    // print job is ejected from the printer, and b) the next print job starts
    // at the top left corner of the page.

    fin.close(); // Close file./
    cout << "\n\n\t\t\t\Printing...";
    cout << "\n\n\ Please press the [ENTER] key to return";
    getch();

    return;
}


Please help me.
Posted
Updated 6-Mar-10 22:45pm
v4

usb001: is not a stream file and I do not think you can write to printers in this way. You should investigate the PrintDlgEx[^] function for details on how to output to a printer device context.
 
Share this answer
 
I want to use turbo C++ for this, not VC++.
 
Share this answer
 
v2
Sorry I don't know turbo C; are you sure that it supports this method of printing?
 
Share this answer
 
Since you indicate that you are using Turbo C++, I presume that you are also writing for Windows. It would be better if you edited your question to include this information rather than posting it in an "answer".

Richard MacCutchan mentioned PrintDlgEx. This is part of the Windows API. It is not specific to VC++. However, it only goes back to Windows 2000 and PrintDlg, only goes back to Windows 95. I believe your Turbo C++ is a windows 3.1 era compiler, so access to these would not have be included with your compiler. For that matter, USB only goes back to 1996, so it wasn't around then either.

The real point here is that normal, run-of-the-mill windows printing is done differently than what you are trying. With the right setup it may still be possible to do something like that, but it is not normally done.

Instead you might consider learning the windows way of printing. It is more involved for the most basic printing, but much easier for anything beyond that. A good book on windows programming should have a chapter on it. And while the specific item Richard mentioned is more recent, most of this stuff does go back to windows 3.0.

No, I'm wrong. Your program is using main, not WinMain. Given the era of your compiler, you appear to be writing a DOS program with a DOS compiler. There is no support for interacting with USB printers. You will probably need to use something to fake out your program, pretending to be a parallel port, and routing stuff to the real printer. I recall some versions of windows as being able to "capture" a parallel port and route it to a printer. You will have to try that or find something like this[^] (I haven't used this.)
 
Share this answer
 
v2
friends i think it is not possible to control printer through c++ it could be possible through c++ builder
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900