-
January 1st, 2018, 07:21 AM
#1
[RESOLVED] Print files
I want to write a program to print text files using a printer in C++.(its not a homework).Please can anyone suggest some suitable code.
-
January 1st, 2018, 08:15 AM
#2
Re: Print files
c++ itself doesn't provide any facility to print to a printer. This is done using API's provided by the underlying OS. What OS are you using?
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.4.0)
-
January 1st, 2018, 08:29 AM
#3
-
January 1st, 2018, 08:44 AM
#4
Re: Print files
This topic probably needs to be placed in Visual C++ section, as MFC library have the device context related to printing and thus the answer can be given.
Popular opinion is the greatest lie in the world.
-
January 1st, 2018, 08:49 AM
#5
Re: Print files
 Originally Posted by AvDav
This topic probably needs to be placed in Visual C++ section, as MFC library have the device context related to printing and thus the answer can be given.
Yes, if the OP is using MFC - otherwise into the WinAPI forum.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.4.0)
-
January 1st, 2018, 12:08 PM
#6
Re: Print files
If it is required only to print text files, then you can get the OS via dos commands to do the heavy lifting. First, how is the printer connected to the computer? If it is via USB or the network then you need to set a dos device name for the printer. If the printer is locally attached via a USB, then first you need to share the printer (Devices & Printers/Printer Properties/Sharing and set to share the printer and give it a short name (eg myprinter). Then in a command prompt
Code:
net use lpt1: \\<comp_name>\<print_name> /persistent:yes
where <comp_name> is the name of the computer to which the printer is attached and <print_name> is the name given to the printer share (eg myprinter). /persistent means always make this connection, so this command only has to be done once.
Assuming this command completes OK, you should then be able to print the text file (eg myfile.txt) from the command prompt by
Assuming this works, then doing this from within a c++ program is trivial.
Code:
system ("print myfile.txt");
If you have trouble getting the dos commands to work, then do an internet search for printing from the command prompt. There's lots of stuff out there about this.
Last edited by 2kaud; January 1st, 2018 at 12:11 PM.
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.4.0)
-
January 1st, 2018, 07:58 PM
#7
-
January 1st, 2018, 08:28 PM
#8
Re: Print files
Is there any way with which we can launch files using c ++? (Means open them from there location )
-
January 2nd, 2018, 04:55 AM
#9
Re: Print files
Launch programs (.exe)? Yes. There are several. system(), CreateProcess(), ShellExecute(), WinExec() and the exec??() family of functions. For detailed info about these there's plenty of info about them on Microsoft's MSDN site.
To open a file (eg .txt etc), then you'll need to launch a program that handles the file type required (eg notepad for .txt file).
All advice is offered in good faith only. All my code is tested (unless stated explicitly otherwise) with the latest version of Microsoft Visual Studio (using the supported features of the latest standard) and is offered as examples only - not as production quality. I cannot offer advice regarding any other c/c++ compiler/IDE or incompatibilities with VS. You are ultimately responsible for the effects of your programs and the integrity of the machines they run on. Anything I post, code snippets, advice, etc is licensed as Public Domain https://creativecommons.org/publicdomain/zero/1.0/ and can be used without reference or acknowledgement. Also note that I only provide advice and guidance via the forums - and not via private messages!
C++17 Compiler: Microsoft VS2019 (16.4.0)
-
January 2nd, 2018, 05:07 AM
#10
Re: Print files
Thanks for that, I am sure to gift you some repotution.
-
January 5th, 2018, 02:32 PM
#11
Re: Print files
 Originally Posted by A_Singh
Thanks for that, I am sure to gift you some repotution.
You may want to try to google some of these well known Windows API questions. It might take a bit longer initially, but then your google kung fu will become strong. You will quickly become self sufficient and won't have to wait for forum answers.
P.S. Probably 99% of the folks answering the questions here don't remember the exact answer and google to confirm. Why not cut out the middleman?
-
January 6th, 2018, 07:44 AM
#12
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
On-Demand Webinars (sponsored)
|