dcsimg
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12

Thread: [RESOLVED] Print files

  1. #1
    Join Date
    Mar 2017
    Posts
    105

    [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.

  2. #2
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,919

    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)

  3. #3
    Join Date
    Mar 2017
    Posts
    105

    Re: Print files

    Windows 7

  4. #4
    Join Date
    Jun 2003
    Location
    Armenia, Yerevan
    Posts
    720

    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.

  5. #5
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,919

    Re: Print files

    Quote Originally Posted by AvDav View Post
    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)

  6. #6
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,919

    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
    Code:
    print myfile.txt
    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)

  7. #7
    Join Date
    Mar 2017
    Posts
    105

    Re: Print files

    Thanks so much 2kaud.

  8. #8
    Join Date
    Mar 2017
    Posts
    105

    Re: Print files

    Is there any way with which we can launch files using c ++? (Means open them from there location )

  9. #9
    2kaud's Avatar
    2kaud is offline Super Moderator Power Poster
    Join Date
    Dec 2012
    Location
    England
    Posts
    6,919

    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)

  10. #10
    Join Date
    Mar 2017
    Posts
    105

    Re: Print files

    Thanks for that, I am sure to gift you some repotution.

  11. #11
    Arjay's Avatar
    Arjay is offline Moderator / EX MS MVP Power Poster
    Join Date
    Aug 2004
    Posts
    13,172

    Re: Print files

    Quote Originally Posted by A_Singh View Post
    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?

  12. #12
    Join Date
    Mar 2017
    Posts
    105

    Re: Print files

    Thanks bro.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Windows Mobile Development Center


Click Here to Expand Forum to Full Width




On-Demand Webinars (sponsored)