Thread: using special filenames for hardware devices eg-"prn" & "con"

  1. #1
    Registered User
    Join Date
    Dec 2001
    Posts
    104

    using special filenames for hardware devices eg-"prn" & "con"

    I've been testing this program, but it doesn't work iether using "prn", "lpt1", or "con". I'm using windowsXP and Borland compiler. Any explainations?
    Code:
    // ezprint.cpp
    // demonstrates simple output to printer
    #include <fstream>                //for file streams
    using namespace std;
    
    int main()
       {
       char* s1 = "\nToday's winning number is ";
       int   n1 = 17982;
    
       ofstream outfile;              //make a file
       outfile.open("PRN");           //open it for printer
    //   outfile.open("con");           //open it for console
       outfile << s1 << n1 << endl;   //send data to printer
       outfile << '\x0C';             //formfeed to eject page
       return 0;
       }

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > Any explainations?
    yeah, those were the DOS device names.

    Since you're not even using DOS anymore (win32 consoles are not DOS either, despite initial similarity).

    Nor did you say which version of borland compiler you're using.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    ...

    And the Windows Device names are...
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  4. #4
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Salem-
    Nor did you say which version of borland compiler you're using.
    Well, I'm using Borland C++ Builder 6.
    The book explaination said that you should be able to run this
    from a console-mode. You say this isn't so?
    neandrake-
    And the Windows Device names are...
    What are the windows device names?

  5. #5
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    DOS device names are still supported in Win2K, probably XP as well.

    I have COM1 connected to COM2 via a null-modem adapter. I have HyperTerminal open on COM2 at 9600, 8N1. This application:
    Code:
    #include <iostream>
    #include <fstream>
    using namespace std;
    
    int main()
    {
        ofstream out("COM1");
        if (!out)
        {
            cerr << "Failed to open COM1" << endl;
            return 1;
        }//if
    
        out << "Hello World";
        out.close();
    
        return 0;
    }//main
    produced the text "Hello World" in the HyperTerminal window.

    You can accomplish the same thing with:
    Code:
    C:\>echo Hello World >> COM1
    - You can't open "CON" for output because it is an input device.
    - "PRN" is just another name for "LPT1", so just use "LPTn" directly.
    - You can test how things will look on the printer with "echo text >> LPT1" on the command line.

    gg

  6. #6
    30 Helens Agree neandrake's Avatar
    Join Date
    Jan 2002
    Posts
    640
    I got your AIM msg, I was at class :/

    I'd like to see the class though.
    Environment: OS X, GCC / G++
    Codes: Java, C#, C/C++
    AOL IM: neandrake, Email: neandrake (at) gmail (dot) com

  7. #7
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Posted it here.

    gg

  8. #8
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    Codeplug-
    Thank you.
    I tried the example you provided, but that doesn't work either.
    The echo command does work from the DOS prompt.
    Code:
    c:\echo Hello World>>lpt1

  9. #9
    Registered User Codeplug's Avatar
    Join Date
    Mar 2003
    Posts
    4,981
    Originally posted by kes103
    I tried the example you provided...
    Did you change COM1 to LPT1?
    If so, then Borland's fstream implementation may not handle DOS device names properly.
    Is there a file named "LPT1" that contains "Hello World" when you run my code with LPT1?

    gg

  10. #10
    Registered User
    Join Date
    Dec 2001
    Posts
    104
    gg-
    Yes, I substituted LPT1 for COM1 in your code.
    No, there is no LPT1 file created when I run your code.

Popular pages Recent additions subscribe to a feed