You are here:

C++/how to take printout of screen


QUESTION: respected sir,

i have made an program in c. and i want to create a option so that i can have a printout of my output screen from printer.
and i don't want to use the print screen from printer. actually i want to give a command or option in the program itself. is it possible...

if u can help me i will be very thank full to you

thanking you

rajesh kumar

ANSWER: The only way I can think to do this is to log all screen output to an array of screen lines you keep in memory.  When the user wants to print the output, you send the saved lines in the array to the printer.


---------- FOLLOW-UP ----------

QUESTION: can you tell me how can i do that or give me some example. actually i am not that much expert.

i will be very thank full to you.

You can use sprintf to put the output into a line buffer.  printf the lines as you add them to the ring buffer, and when you want to print, write to the printer the number of lines you choose to buffer.

I would use a variable argument printf, like vsprintf, so that you can call a function as printf is called, store into the buffer and write to the screen.  Ok, here, it's easier to write code than explain it.  The following will buffer 80 lines (forever) and printScreen() will print the last 80 lines.  You need to find how to write a line to the printer.  I did the rest for you.  There is no error checking - you should add some.  256 bytes per line may not be enough (although a printed page only has 80 to 120 characters).

#define LINES_TO_BUFFER 80

char  lines[LINES_TO_BUFFER][256];
int lineCount;

void  output( const char *fmt, ... )
va_list va;

 va_start( va, fmt );
 vsprintf( lines[lineCount], fmt, va );
 va_end( va );
 printf( lines[lineCount] );
 if( ++lineCount == LINES_TO_BUFFER )
   lineCount = 0;

void printScreen( void )
int count;

 count = lineCount;
 do  {
   if( lines[ count ][ 0 ] )
     // HERE => write line[ count ] to printer!
   count = ++count % LINES_TO_BUFFER;
 } while( count != lineCount );



All Answers

Answers by Expert:

Ask Experts


Bill A


I can answer questions about C++, programming algorithms, Windows programming in MFC (which is C++). I cannot answer questions about STL (templates) and I have no experience with Linux. I do enjoy reviewing code and critiquing it or finding problems in it. I will also gladly show better algorithms or methods if you want to take advantage of that.


I've developed a commercial embedded C compiler/assembler and IDE with debugger toolset, of which the IDE and debugger are written in C++. I work in the industry writing high tech embedded programs and Windows programs to communicate with the embedded devices.

Book: Embedded Systems Design using the Rabbit 3000 Microprocessor Authored Chapter 10 in its entirety.

BS Computer Engineering

©2017 All rights reserved.