Groups | Blog | Home
all groups > visual c libraries > november 2004 >

visual c libraries : How to print a txt file into a printer?



Mark
11/10/2004 1:55:01 AM
i can print a string into a printer,for example

FILE *pFile = fopen("LPT1", "w");
fprintf(pFile, "i like c++");
fclose(pFile);

But now i want to print a txt file into a printer, what should i do?
LhK-Soft
11/11/2004 1:28:05 AM
Hi,

[quoted text, click to view]

It's easy, but requires some more development. Look into the example source
below:

FILE* prnOut = fopen("LPT1", "wt"); // using as 'text'
FILE* txtIn = fopen("MyTextfile.txt", "rt"); // using cr-lf translation
// do some testing here that both files realy are opened:
if ((!prnOut) || (ferror(prnOut)) || (!txtIn) || (ferror(txtIn)))
{
if (prnOut) fclose(prnOut);
if (txtIn) fclose(txtIn);
return;
}
int hasread;
char buf[2048]; // 2KB bufsize
while ((!ferror(prnOut)) && (!ferror(txtIn)) && (!feof(txtIn)))
{
hasread=fread(buf, sizeof(char), 2048, txtIn);
if (fwrite(buf, sizeof(char), hasread, prnOut) != hasread)
break; // properly an error occured...
}
//...

Good luck!
AddThis Social Bookmark Button