Hi
//THIS TUTORIAL WAS MADE BY HANDzCLEANx
AND IS PROTECTED UNDER ARTICAL M$43:"Monopilising the System"//
NOTE:This is printing in console-mode for BEGINNERS
This tut is my own work,if you want to use it,just.....bah whats the point,steal it!

After asking for help on printing in C++ here here and not recieving any, and after
searching a bit on the net and library files i found gold,and decided to share it with AO.

Skip to the bottum/ passed theTHEORY-****(wich is very-important) to see the code in use.


So here goes:
HOW TO PRINT IN C/C++.
1st)WHAT IS A PRINTER??
This is where OO-THINKING(object orientated thinking) comes in. If you think its just a thing that
puts ink to paper,then you're thinking SUBJECT-ORIENTATED and then you are wrong,
but
if you are thinking of it as a device that's "part" of the SYSTEM and just have some
specialised functions like say, printing etc.
Same with any other device like the modem,LAN,etc, thats more OO,.

We know now: <1>The printer is a device.



2nd).....SO,HOW DO ACCESS IT??
Ask yourself this:
Where is the printer??-> ITS ON THE DESK.
What is used for them to communicate??->"ELECTRICITY".
How is the printer connected to the PC??->via a CABLE.
Where is the cable connected/placed??->IN A PORT/thingy-with-holes BEHIND THE COMPUTER.
Identify that port->Its a port named: LPT1(assumebly,else:LPT2,USB).

Lets wrap it up, the printer:
Is a DEVICE on the desk,it streams ELECTRICITY via a CABLE, into a
PORT/thingy-with-holes
that can effectivly be identified as: LPT1/LPT2/USB.

AND THAT IS WHAT YOU NEED TO KNOW ABOUT THE PRINTER
and we continue..

For windows-printing I dont know(some API library),
but
for console printing there is a STANDARD library namly STDIO.h, wich as you know
is the STANDARD INPUT OUTPUT library,wich contains the infamous printf fucntion,to name one.
Since this is only for console-mode programs,and the printf is problably the type of
output-function most used, i'll show only the printf type of printing.

If you take a quick look at the STDIO.h library in
your fav compiler(i use Bloodshed DEV-C++),
you will find the following from line 199 and onwards :

/*
* Formatted Output
*/

int fprintf (FILE* filePrintTo, const char* szFormat, ...);
int printf (const char* szFormat, ...);
int sprintf (char* caBuffer, const char* szFormat, ...);
int _snprintf (char* caBuffer, size_t n, const char* szFormat, ...);
int vfprintf (FILE* filePrintTo, const char* szFormat, va_list varg);
int vprintf (const char* szFormat, va_list varg);
int vsprintf (char* caBuffer, const char* szFormat, va_list varg);
int _vsnprintf (char* caBuffer, size_t n, const char* szFormat,
va_list varg);


as you can see the printf-series is expanded.

All we need to do is find the PICK the right one.

ASK YOURSELF:
1st)WHAT DO WE WANT??->We want to use a printf type.
-We know printf:
int printf (const char* szFormat, ...);

cant do the job since there is no way of directing the "flow" to the anywhere else than
the standard output,wich is the screen.

what about fprintf:
int fprintf (FILE* filePrintTo, const char* szFormat, ...);
-its like printf ,but you can direct it to somewhere else of type FILE*.
Remember FILE* is not necerilly a file.

Now we can direct it.

RECAP: The fprintf() fucntion in the STDIO.h library
can be used to direct output to somewhere else,say like,
the printer.

Enough THEORY lets start with the CODE:

//COMMENTS WILL BE INDICATED ASLIKE C/C++
//START OF PROGRAM
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main(void){
FILE *printer;

//A FILE POINTER NAMED printer WAS
//MADE,NOW WE WILL OPEN OUR PRINTER-PORT TO BE WRITTEN
//IN BINARY-mode, REPRESENTED BY printer
printer= fopen("lpt1","b+w");
//lpt1 and lpt2 works fine in DEV-C++
//in Borland c++ its something like: stdprnt or stdlpt1 or stdlpt2.
//NOW WE USE fprintf() TO DIRECT OUTPUT TO print
//fprintf() HAS THE SAME GRAMMER AS printf
//EXCEPT THAT A FILE POINTER IS INSERTED AS THE 1st ARGUMENT

fprintf(printer,"Hello printer-world\n \r 2nd line \n");
//REMEMBER TO USE \r (CARRIAGE RETURN)
//to align the text to the left
//REMEMBER TO USE ALOT OF \n(NEWLINE)'s
//because the printer ignores everything after the last \n

fclose(printer);

//AND WE CLOSE THE FILE* AND WAIT FOR THE PRINT.....


}

//END OF CODE

IF you want the a pure workable code here:
#include <iostream.h>
#include <stdlib.h>
#include <stdio.h>

int main(){

FILE *print;

char portname[10];
portname = "lpt1";
print=fopen(portname,"w+b")
fprintf(print,"hello printer");
fclose
}

This code works fine in DEV-C++ compiler

there is a known issue with what the port should be identified by,some compilers just use diffrent names
let me show you a few:
portname="stdprt";
or
portname="lpt1";
or
portname="prnt";
or
portname="stdprnt";
or
portname="lpt2";
or
portname="stdlpt1"
or
portname="stdlpt2";



So there you have it,try experimenting with all the output functions that supports the
FILE-pointer like fputc,etc.

Sorry for the spelling.Some terms used might not be correct.
IF THERE IS ANYTHING THAT YOU DONT UNDERSTAND PLEASE PM ME

...................no printers were hurt in the making of this thread.............