print a text file

I want to print a text document using c++ .
anybody can tell me how is it possible.
1
2
3
4
5
6
7
8
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file( "whatever.txt" ) ;
    std::cout << file.rdbuf() ;
}
Last edited on
You mean physically print onto paper, not print as in standard output, right?
JLBorges, you would actually need std::ofstream not std::ifstream

Ifstream is an input file, not output.
@paulthepenguin, he reads the text document from file so he correctly using std::ifstream.
yes i want to print it physically on paper.
In standard C++ doesn't know anything about printers. You have to use a library or some platform dependent way to do it.

On windows I guess you can use the WinAPI.
> yes i want to print it physically on paper.

Use the appropriate name on your platform to open the printer as a file. This will bypass the spooler.

For example, on Unix, if the printer is mounted on /dev/lp0
1
2
3
4
5
6
7
8
9
#include <iostream>
#include <fstream>

int main()
{
    std::ifstream file( "whatever.txt" ) ;
    std::ofstream printer( "/dev/lp0" ) ;
    printer << file.rdbuf() ;
}


I've read that something like this works on Windows: std::ofstream printer( "LPT1:" ) ;
Topic archived. No new replies allowed.