can anyone help to print to printer in c++?

Discussion in 'C++' started by snagaraj85, Jan 12, 2016.

  1. snagaraj85

    snagaraj85 New Member

    Joined:
    Jun 4, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Dear Friends,

    can anyone help to print a text to default printer in c++?
    ex:-

    Code:
    #include <iostream>
    using namespace std;
     int main()
    {
    cout<<"Hello World!!!"<<endl;
    return 0;
    }
    
    in the above example i would like to print the Hello World!!! in a physical printer connected to my system. pls share the sample code.
     
  2. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    What operating system are you using? It's different for each one.
    And what API are you using? It is also different for each one of those.
     
  3. snagaraj85

    snagaraj85 New Member

    Joined:
    Jun 4, 2015
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    i'm using Windows 7 as the OS, and using Dev C++ compiler environment, please i'm new to programming and update the above code post it.
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    If you're new to programming then I'm pretty certain this'll be beyond you. Your best bet is to write to a file, open it in Notepad, then use the File Print menu from there.

    If you don't believe me then have a look at the code here:
    https://support.microsoft.com/en-us/kb/186736

    When you can understand what that code is doing, then you'll be able to adapt it to your needs.
     
  5. alia123

    alia123 New Member

    Joined:
    Jan 8, 2016
    Messages:
    65
    Likes Received:
    5
    Trophy Points:
    0
    Hey, try this code :
    Code:
     #include<iostream.h>
     #include<fstream.h>
     #include<stdlib.h>
     #include<conio.h>
    
     main(int argc,char *argv[])
        {
           clrscr();
    
           if(argc!=2)
          {
             cout<<"\n Format : File - Print File Name "<<endl;
             exit(0);
          }
    
           char ch='\0';
    
           ifstream input_file;
           ofstream output_file;
    
           input_file.open(argv[1]);
           output_file.open("PRN");   // or LPT1while(input_file.get(ch)!=0);
    
           output_file.put(ch);
    
           getch();
           return 0;
        }
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice