Thread: Printer out from C++

  1. #1
    Registered User
    Join Date
    Apr 2007
    Posts
    2

    Printer out from C++

    I am using DEV-C++ and found a listing called "Print_it.c in "Teach Yourself C in 21 Days" on page Listing T&R 1. Listing Follows.

    Code:
    /*print_it.c-This program preints a listing with line numbers */
    
    #include <stdio.h>
    #include <stdlib.h>
    
    void do_heading(char *filename);
    
    int line = 0, page = 0;
    
    int main( int argv, char *argc[] )
    {
    
     char buffer[256];
    
     FILE *fp;
    
     if( argv < 2 )
     {
      fprintf(stderr, "\nProper Usage is: ");
      fprintf(stderr, "\nprint_it filename.ext\n" );
      return(1);
     }
    
     if (( fp = fopen(argc[1], "r" )) == NULL )
     {
      fprintf( stderr, "Error opening file %s!", argc[1]);
      return(1);
     }
    
     page = 0;
     line = 1;
     do_heading( argc[1]);
    
     while( fgets( buffer, 256, fp ) != NULL )
     {
      if( line % 55 == 0 )
         do_heading(argc[1] );
         fprintf(stdprn, "%4:\t%s" , line++, buffer );
     }
    
        fprintf( stdprn, "\f" );
        fclose(fp);
        return 0;
    }
    
    
    void do_heading( char *filename )
    {
     page++;
    
     if ( page > 1 );
        fprintf(stdprn, "f" );
    
        fprintf( stdprn, "Page: %d, %s\n\n", page, filename );
    }
    
          system("PAUSE");
          return 0;
    }
    This program will not compile as stdprn is not defined Any assistance woild be creatly appreciated with what header file I need or what Define syntax I need to get this listint to work.

    Dick

  2. #2
    Devil's Advocate SlyMaelstrom's Avatar
    Join Date
    May 2004
    Location
    Out of scope
    Posts
    4,079
    If you're teaching yourself C, then why are you asking in the C++ forum? I don't know that stdprn references a valid file descriptor for any given Operating System (or any modern operating system, for that matter), but in both C and C++, I'm quite sure there are more modern methods of printing something. With today's printers you likely need to communicate with their drivers in order to send data to them, depending on your OS, there will be different ways of doing this. Also, check the FAQs on why system() is bad.
    Sent from my iPadŽ

  3. #3
    Registered User
    Join Date
    Apr 2007
    Posts
    2
    Maybe I'm asking my question wrong! When writing a C++ program; How to you access your printer?

    Dick

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Perhaps you could try

    FILE *toPrinter = fopen("LPT1","w");
    or other variations like "LPT:", "PRN" or "PRN:"

    Check that the open was successful, and that you don't get a file created with the name you choose.
    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.

  5. #5
    Registered User
    Join Date
    May 2008
    Posts
    1
    about 15 years ago I had a compiler from Mix Software that recognized stdprn. It's been awhile, but as I remember you had to print to a file and then print the file. I could be wrong about the process but I had managed to create a check writing program using stdprn and print checkbook size checks. Le me know if this helps.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 01:02 PM
  2. changing property (layout) of a printer
    By leojose in forum Windows Programming
    Replies: 5
    Last Post: 12-05-2005, 07:16 AM
  3. printer gone crazy
    By DavidP in forum Tech Board
    Replies: 2
    Last Post: 03-29-2004, 01:16 AM
  4. Success - Output to Printer
    By noraa in forum C Programming
    Replies: 5
    Last Post: 08-04-2002, 09:12 AM
  5. Replies: 0
    Last Post: 04-30-2002, 07:24 PM