-
printing in 'C'
Can somebody please help me? I am a student and I have to write a program that read from a file (which I can do), then print this information to paper in a formatted way. Please can someone tell me the function to use and also include a small description of how to use it. Thanks to all who respond.
-
The Easiest way out is to format the text or whatever as u like and then save it to a file (maybe a temporary file) and close the file. Then invoke the system command (Im specifically talking of the DOS environment). Just say...
system("type filename.ext >> PRN");
where filename.ext is the name of the temporary file that you want to print (which is already formatted). I just issued a type command (a DOS internal command and redirected the output to the Standard Printer).
*Include the header file process.h
Regards,
Sriharsha.
-
The format does not fit on the screen, the screen is too narrow. So how can i format the layout to a file?
-
Use fprintf() function to print the formatted lists into ur file. (dont display them on screen. U first analyze the layout you want the data to be in and then use fprintf()... which is same as printf but it prints to files, and save the file.).
Regards,
Sriharsha
-
I need to print to paper in landscape mode how is that done, also my file is a binary file not text. help!!!! (again).
Thanks.
-
Do you have an attempt?...I will help if I can see an attempt.
-kris
-
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *printer = fopen("LPT1","w");
FILE *fp;
char str[256];
char buf[BUFSIZ];
printf("File name? ");
scanf("%s",str);
fp = fopen(str,"r");
if(!fp)
{
printf("File does not exist\n");
return -1;
}
fgets(buf,BUFSIZ,fp);
while(buf[0])
{
fprintf(printer,"%s",buf);
fgets(buf,BUFSIZ,fp);
}
fprintf(printer,"\f");
return 0;
}
This code was originally posted by someone I don't remember :).
The code was reworked by Prelude into this:
Code:
#include <stdio.h>
#include <stdlib.h>
int main(void)
{
FILE *printer = fopen("LPT1","w");
FILE *fp;
char str[256];
char buf[BUFSIZ];
printf("File name? ");
scanf("%s",str);
fp = fopen(str,"r");
if(!fp)
{
printf("File does not exist\n");
return -1;
}
fgets(buf,sizeof buf,fp);
while(buf[0])
{
fprintf(printer,"%s",buf);
fgets(buf,sizeof buf,fp);
}
fprintf(printer,"\f");
return 0;
}
I used the source code for this file as a printing test ( as it resides in the same directory, hopefully ). The printer prints your file just fine. However, it then dives into an endless loop of right code braces.
> char buf[BUFSIZ];
Do I need to change the BUFSIZ into sizeof buf to match Prelude's change?
-
> However, it then dives into an endless loop of right code braces
Because of your faulty attempt to detect the end of file, you endlessly print the last line of the file
Code:
while( fgets(buf,BUFSIZ,fp) != NULL ) {
fprintf(printer,"%s",buf);
}
> Do I need to change the BUFSIZ into sizeof buf to match Prelude's change
No
-
Ahh, I see. Thanks again Salem. Works like a charm...
-
Thanks for responding everyone, i will post an attempt.
I still will probably need more help - and i will try harder too.
Thanks again
-
>The code was reworked by Prelude into this:
That's not much of a change, was I trying to make another point at the time? Because I would have made a few more modifications otherwise. ;)
-Prelude
-
I modified quite a few thnigs myself. It works for me no matter how many different ways I stab at it. I did get that from another post as you realize, however.
-
Anybody has any ideas on how to print in C using a usb printer?
-
Actually nutshell and the rest i have a usb printer i didnt think there was going to be a difference besides the connection, more help needed i think. damn.
Also how do you print a certain piece of data at a certain position on the paper, then another piece of data at another position on the paper???
-
Wow, I just found these forums, quite handy in deed! This was a problem I tried dealing with about a year ago, never figured out how to get a file to print, but then just gave up trying since I didn't really care. I knew it was supposed to be easy, and looking at this it is pretty easy.
Anyway, to try and answer your question. You can selectively pick out portions of the file. You need to use fseek (? right ?), but I haven't done that in well over a year. If I remember correctly, it's relatively simple, as long as you provide the correct info. Just like your standard fread you need to provide the data size of what you are reading in, where you want it to read to, all the standard stuff, PLUS how many bytes to seek into the file. In other words, you need to pretty much need to know where the data resides in the file so that you can tell the program where to look.
Anybody, is this correct? I've only once read in a file radomly, and can't rember exactly how to do it.