#include <iostream>
#include <stdio.h>
usingnamespace std;
int maint(){
int num1;
cout << "Enter a number:";
cin >> num1;
if(num1 == 1){
fprintf(stdprn,"This is a test to print %s", "string");
return 0;
}
}
i'm trying to print the output "This is a test to print" and link it to printer..but the error occur....someone helps me fix this...
stdprn isn't part of standard C. Some compilers (don't know which) that are tightly coupled with the OS seem to offer this, but it doesn't look like your compiler is one of them.
As I mentioned above, stdprn isn't part of C++ (or even C). It was a language "add-on" done by one of the compiler makers years ago, and will only work with their compiler. The fact that you're including <stdio.h> and it's still undeclared, is strong evidence that your set-up doesn't support stdprn.
Standard C/C++ is designed to produce highly portable code. As such, it doesn't assume anything about your system or devices; this is why stdprn capability doesn't exist in most implementations.
So, you have a few choices:
1. write the output to a file, and print the file after your program exits. This approach isn't very automated, but it should result in the most portable code.
2. alter your program to open a stream to your printer device. I can't give you much help on this, so some googling may help you.
3. write the output to a file, and at the end of your program, use a system() call to print the file, then delete the file. This is really not recommended, as the system() call parameters will result in highly unportable code. system() is also an "expensive" function (in terms of CPU) but that probably doesn't matter if you're on a PC.
Let us know what you think of these options, after you look at them a bit.
well...i would like to choose option 2 and implement it in my project..but so far...i can't really find the way to connect to my printer port...this is the sample code...but i don't know whether it work or not...
I can't tell you whether it will work or not. I'd need to know more about your printer, and I'm really not the best guy to tackle those questions anyway. If you're using a modern printer, though, I don't know if they accept simple text and just print it out, or if they require a bunch of set-up codes and stuff.
Regarding your code above, it looks good, but you may want to check the fail bit after your open() call, to make sure the stream at least opened.
I don't think I'm going to be much help to you from here on out, but good luck and keep us posted.
C++ knows nothing of printers. Nothing. It also knows nothing about keyboards, monitors or the mouse. Everything to do with that requires some kind of interaction with your OS. What OS are you using?
stdprn was an MS-DOS stream. I guess you're not using MS-DOS. This used to be a Win32 equivalent: