Link to home
Start Free TrialLog in
Avatar of whatemail
whatemail

asked on

how to print output and source to a printer Borland C++ 5.02

Hi,

I just bought Borland C++ 5.02 (not VC++, but the text/command based one) to learn how to program in C++. But I can not get any programs that I write to output to a printer.

For example, this does not work:
fprintf (stdprn, "%d\n", i);

Gives an error of:
"Undefined symbol stdprn

It was suggested that i use a stream, like the following.  This does work but I would like to know why the stdprn does not?  Is there a simpler way to do this?

char *PortName="LPT1:";
FILE *Printer=fopen(PortName,"wb");
fprintf(Printer,"Text:\r\n\n",m_var);



Also, is there a way to have my program output the results of the program and all the source code for itself automatically after being run?

I would appreciate your help on this problem as I am stuck.

Thank you.
Avatar of senohp
senohp

The stdprn stream is not defined by MS Windows / NT. Set the target project to DOS (not windows) and you will find your code compiled well.

regards

Seno
Avatar of whatemail

ASKER

Senohp,

Unfortunately you will have to walk me through even that.  I am very new to working with C++ and especially in this environment.  How would i set the target project to DOS (not windows)?  And any ideas on printing out the source code through a command in the program itself?

Sorry, just a beginner.
1.stdprn you can use in Dos/Console Windows apps.
Dos is dead, so you may use it in console.
For make Console project , goto View|Project, set
mouse to name of you exe, right button, and chouse
TargetExpert and :
Win32 in Platform Window
Console in Target Model Window

2.If you make new project: File|New|Project and again
 as in 1.
ASKER CERTIFIED SOLUTION
Avatar of kulina
kulina

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
>>is there a way to have my program output the results of >>the program and all the source code for
>>itself automatically after being run?

Use the next example for printing output to printer:

/* demonstrates how to print programs output to printer */

#include <stdio.h>
#include <stdlib.h>

FILE *printer;

int main( )
{
    char input[255];
    printer = fopen( "LPT1", "w" );

    if( !printer ){
        printf( "Can't open connection to printer\n" );
        exit( 2 );
    }

     fprintf( printer, "Some output...\n\r" );
     fprintf( printer, "more output...\n\r" );
     fprintf( printer, "and some more...\n\r" );    
    fprintf( printer, "keep coming...\n\r" );
    fprintf( stdout, "Enter something and press enter: " );
    fflush( stdout );
    gets( input );
    fprintf( printer, "entered by user: " );
    fprintf( printer, input );
    fprintf( printer, "\n\r" );

    /* At this point use function printTextFile from previous */
    /* example to print your source code file */

    /* option "\f" is neccessary to eject the sheet */
    fprintf( printer, "ok stop right here and eject the sheet\n\r\f" );
    fflush( printer );
    fclose( printer );
}

Good luck.
Another way of printing your output to printer. It wokrs in similar fashion command line redirection i.e. "ls -al > log.txt" on Unix or "dir > log.txt" on DOS,. etc.


/* Demonstrates how to redirect standard output to printer */
/* Everything you print using standard functions such as printf, puts, etc */
/* will be sent to printer. Redirection is implemented using dup and dup2 */
/* functions. Don't forget \r when printing to printer, and \f when you */
/* want to eject the sheet. */

#include <stdio.h>
#include <stdlib.h>
#include <io.h>
#include <fcntl.h>

#ifndef O_WRONLY
    #define O_WRONLY 1
#endif
#define _STDOUT_ 1

void main()
{
    char input[255];
    int old_stdout;
    int printer;

    printf( "This prints on a standard output\n" );

    printer = open( "PRN", O_WRONLY );
    old_stdout = dup( _STDOUT_ );          /* "old" now refers to "stdout" */
    dup2( printer, _STDOUT_ );             /* make "printer" stdout */

    puts( "This should go to the printer\r" );
    printf( "This, too\n\r\f" );
    fflush( stdout );
   
    dup2( old_stdout, _STDOUT_ ); /* restore "stdout" to its original state */
    close( printer );

    printf( "Printing again on standard output\n" );
}
Thanks Kulina,

I just wanted to let you know that I am going to have to wait until Monday afternoon to test this out, so i will accept the answers then if they work.  Sorry for the delay.
Sure, let me know if you need some explanation of code examples I posted above.
Regards.
Thanks for the help.  Sorry for the delay, but i boosted the pts up to 75 to make up for it.

But thank again.
You're welcome and thank you, too. I am extra glad to be of help to appreciative members.
Cheers!