-
format for printer
Hi, a question about format for printing a report to the printer. how do we use C to format a printed report.....
Say we have the report headings
Code:
Department Weekly Expenses 99 99 99
printed across page second line down...How do we use fprintf to format this. I was thinking along the lines of .....
Code:
FILE *prnt;
prnt = fopen("PRN", "w");
fprintf(prnt,"\n%50s %02d %02d %02d\n",
"Department Weekly Expenses", day, month, year);
remembering grid for print page is 110 by 42.
Any advice would be greatly appreciated...
-
> How do we use fprintf to format this. I was thinking along the lines of .....
Well your suggestion would work.
It's really a question of how much of this formatting do you have to do, and how often someone else will ask you 'move this line down 1', or 'move this word 2 spaces left' etc.
If you're going to have to do this, then the fprintf approach becomes very tedious to keep up to date.
An alternative would be to declare a couple of functions which handled the white space. This at least separates the positioning of the text from the printing of the text.
void lines ( FILE *fp, int nlines );
void spaces ( FILE *fp, int nspaces );
Then your example becomes
Code:
lines( prnt, 1 ) ; spaces ( prnt, 30 );
fprintf(prnt,"Department Weekly Expenses %02d %02d %02d\n", day, month, year);
> remembering grid for print page is 110 by 42.
An alternative approach would be to declare
char print_page[42][111];
and just write characters to the correct place in the array as required.
Of course, to make it easy to use, you would need to implement a number of functions to do some of the detail for you, for instance
void centre_line ( int line, char *string, ... );
would centre the text on the specified line. To keep all the flexibility of printf, you can easily make them all variadic functions.
Add a couple of functions to init_page and print_page, and you could be on your way.
This approach requires much more up front investment in coding time, but could pay off by being much easier to use when you're done (easier to generate new reports, and modify existing ones).