Thread: format for printer

  1. #1
    Registered User
    Join Date
    Aug 2001
    Posts
    247

    Question 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...
    hoping to be certified (programming in c)
    here's the news - I'm officially certified.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,656
    > 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).
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. GradeInfo
    By kirksson in forum C Programming
    Replies: 23
    Last Post: 07-16-2008, 03:27 PM
  2. Compression/Decompression Wave File and MP3
    By cindy_16051988 in forum Projects and Job Recruitment
    Replies: 51
    Last Post: 04-29-2006, 06:25 AM
  3. A Better Format Simulator
    By toonlover in forum C++ Programming
    Replies: 2
    Last Post: 01-07-2006, 06:14 PM
  4. Seeking Format Advice
    By PsychoBrat in forum Game Programming
    Replies: 3
    Last Post: 10-05-2005, 05:41 AM
  5. Can't Format C
    By caroundw5h in forum Tech Board
    Replies: 40
    Last Post: 04-26-2004, 09:57 AM