Thread: printing in 'C'

  1. #1
    Unregistered
    Guest

    Unhappy 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.

  2. #2
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    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.
    Help everyone you can

  3. #3
    Unregistered
    Guest

    Unhappy

    The format does not fit on the screen, the screen is too narrow. So how can i format the layout to a file?

  4. #4
    Im a Capricorn vsriharsha's Avatar
    Join Date
    Feb 2002
    Posts
    192

    Thumbs up

    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
    Help everyone you can

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    Wink

    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.

  6. #6
    Unregistered
    Guest
    Do you have an attempt?...I will help if I can see an attempt.

    -kris

  7. #7
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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?
    The world is waiting. I must leave you now.

  8. #8
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > 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

  9. #9
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Ahh, I see. Thanks again Salem. Works like a charm...
    The world is waiting. I must leave you now.

  10. #10
    Registered User
    Join Date
    Apr 2002
    Posts
    15
    Thanks for responding everyone, i will post an attempt.

    I still will probably need more help - and i will try harder too.

    Thanks again

  11. #11
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >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
    My best code is written with the delete key.

  12. #12
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    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.
    The world is waiting. I must leave you now.

  13. #13
    Registered User Nutshell's Avatar
    Join Date
    Jan 2002
    Posts
    1,020
    Anybody has any ideas on how to print in C using a usb printer?

  14. #14
    Registered User
    Join Date
    Apr 2002
    Posts
    15

    Lightbulb

    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???
    Last edited by ganonshin; 04-21-2002 at 07:52 PM.

  15. #15
    Unregistered
    Guest
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C# Printing Problem
    By silverlight001 in forum C# Programming
    Replies: 0
    Last Post: 03-23-2009, 01:13 AM
  2. generic printing preferences dialog box
    By stanlvw in forum Windows Programming
    Replies: 8
    Last Post: 06-27-2008, 02:20 AM
  3. printing data to a file
    By coralreef in forum C Programming
    Replies: 3
    Last Post: 11-02-2006, 08:10 PM
  4. need help relating printing?
    By omarlodhi in forum Linux Programming
    Replies: 0
    Last Post: 03-03-2006, 04:46 AM
  5. Printing
    By Dual-Catfish in forum A Brief History of Cprogramming.com
    Replies: 3
    Last Post: 05-25-2002, 08:10 PM