Sending to printer

This is a discussion on Sending to printer within the C Programming forums, part of the General Programming Boards category; Originally posted by Shadow Code: /* Program: Printer.c Purpose: Demonstrates how to use the printer in your programs. */ #include ...

  1. #16
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Shadow
    Code:
    /*
    
    	Program:
    	Printer.c
    	
    	Purpose:
    	Demonstrates how to use the printer
    	in your programs.
    	
    */
    
    #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?\n ");
    	 scanf("%s",str);
    	 fp = fopen(str,"r");
    	 if(!fp)
    	 {
    	 	printf("File does not exist\n");
      		return -1;
     	 }
     	 while( fgets(buf,sizeof buf,fp) != NULL ) 
    	 {
    	 	printf("\nPrinting..\n");
      	       	fprintf(printer,"%s",buf);
    	 }
    
     	 fprintf(printer,"\f");
             return 0;
    }
    ....if any of that helps.
    To answer the reply prior to this one, what do you mean by "do I have a file in my directory called 'LPT1'? Do you mean a file in my program or on my computer? My printer file on my computer is Lexmark....but it is conected via 'LPT1' port. Do I need to create a file in my program called 'LPT1'? If so what is the purpose?

    The sample code you just sent me is the same sample I started with. The problem with this is that what I'm trying to do is search my 'client.dat' file which holds strutures containing 'client code'. When it finds the client code, I want to print that structure out on paper. Hers is how I've modified it so far:
    Code:
    /************************************************************/
    char print_label (void)
    /*
      Task: Prints shipping information
    */
    /************************************************************/
    {
    	FILE *printer;
    	FILE *dta_file;
    	char buf[BUFSIZ];
    	int user_inputted_client_code;
    	clrscr();
    	printf("\t\t\tClient Inventory Management \n\n\n");
    	printf("\tPrint Client Label \n\n");
    	printf("Client Code: ");
    	scanf("%d",&user_inputted_client_code);
    	if((dta_file=fopen(FILENAME,"rb")) == NULL)
    	{
    		perror(FILENAME);
    		return(1);
    	}
    	if((printer = fopen("LPT1","w")) == NULL)
    	{
    		perror("LPT1");
    		return (1);
    	}
    	while((fread(&another_client,sizeof(struct fclient),1,dta_file)) == 1)
    	{
    		if(user_inputted_client_code == another_client.client_code)
    		{
    			fgets(buf,sizeof buf,dta_file);
    			while( fgets(buf,BUFSIZ,dta_file) != NULL )
    			{
    				//	fprintf(printer,"printclient2(&another_client)",buf);
    				fprintf(printer,"buf");
    				fprintf(printer,"\f");
    			}
    			//fprintf(printer,"buf");
    			//fprintf(printer,"\f");
    		}
    	}
    	if(!dta_file)
    	{
    		printf("File does not exist\n");
    		return -1;
    	}
        fclose(dta_file);
    	key_wait();
    	return 0;
    }
    /*HERE IS THE INTERNAL FUNCTION BEING CALLED*/
    /************************************************************/
    int printclient2 (struct fclient *client)
    /************************************************************/
    {
    	printf ("Client Code: %d\n\n", client->client_code);
    	printf ("Client Name: %s\n", client->clientname);
    	printf ("Street Address: %s\n", client->street_address);
    	printf ("City: %s\n", client->city);
    	printf ("Province or State: %s\n", client->pros);
    	printf ("Country: %s\n", client->country);
    	printf ("Postal or Zip Code: %s\n\n\n", client->postalorzip_code);
    	return(1);
    }
    Some things are commented out for now because I was trying different options. Also, I didn't think I had any use for 'char str[256]' where as I'm searching for an 'int client_code'. I'm totally stumped.
    All suggestions welcome, but I would prefer keeping the program the same without having to make drastic changes. I need to have this working by tommorrow night at the latest or it's my ass!
    Thx.
    Attached Files Attached Files

  2. #17
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    > what do you mean by "do I have a file in my directory called 'LPT1'?
    I mean

    Code:
    D:\code\test>dir
     Volume in drive D is Apps
     Volume Serial Number is 0429-F1A4
    
     Directory of D:\code\test
    
    27/05/2002  19:35       <DIR>          .
    27/05/2002  19:35       <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)   1,974,091,776 bytes free
    If you have a file called LPT1, then you just created a file, you didn't send anything to the printer, and it wouldn't complain when you tried to write to it

    Other things to try
    DOS used to use a trailing : to signify a device and not a file, so perhaps one of these


    printer = fopen("LPT1","w")
    printer = fopen("LPT1:","w")
    printer = fopen("PRN","w")
    printer = fopen("PRN:","w")

  3. #18
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Salem
    > what do you mean by "do I have a file in my directory called 'LPT1'?
    I mean

    Code:
    D:\code\test>dir
     Volume in drive D is Apps
     Volume Serial Number is 0429-F1A4
    
     Directory of D:\code\test
    
    27/05/2002  19:35       <DIR>          .
    27/05/2002  19:35       <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)   1,974,091,776 bytes free
    If you have a file called LPT1, then you just created a file, you didn't send anything to the printer, and it wouldn't complain when you tried to write to it

    Other things to try
    DOS used to use a trailing : to signify a device and not a file, so perhaps one of these


    printer = fopen("LPT1","w")
    printer = fopen("LPT1:","w")
    printer = fopen("PRN","w")
    printer = fopen("PRN:","w")
    Thx I'll try those and maybe I'll have some luck. Still wondering about my 2nd 'while'. Which way would you assume is correct: the commented out parts or what is showing now?
    Attached Files Attached Files

  4. #19
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,672
    Actually, until your printer is working, I haven't been looking too closely at the finer detail of your code.

    It's not far off, so once it prints, getting it to work shouldn't be a problem

  5. #20
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Salem
    Actually, until your printer is working, I haven't been looking too closely at the finer detail of your code.

    It's not far off, so once it prints, getting it to work shouldn't be a problem
    Until my printer's working? My printer works fine. My code won't open the path to the printer. I tried all the different DOS commands and I think it should probably be 'LPT1:'. In properties of my printer, it's set to "LPT1: (printer port)". Should I include "(printer port)" or would that make any difference? I'm thinking not. Your thoughts?

  6. #21
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by spentdome


    Until my printer's working? My printer works fine. My code won't open the path to the printer. I tried all the different DOS commands and I think it should probably be 'LPT1:'. In properties of my printer, it's set to "LPT1: (printer port)". Should I include "(printer port)" or would that make any difference? I'm thinking not. Your thoughts?
    Also,
    Code:
    while( fgets(buf,BUFSIZ,dta_file) != NULL )
    {
             fprintf(printer,"printclient2(&another_client)",buf);
             fprintf(printer,"\f");
    }
    Should this at least open the printer path and should the printer at least suck paper through even if nothing is printed?

  7. #22
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    Test the program I submitted.
    Compile it and run it.
    Simply enter the full path to a file, with your printer on, and it should print in default mode ( not even, plain text actually ) as if you printed from notepad.

    If this doesn't work, something hasn't been covered.
    The world is waiting. I must leave you now.

  8. #23
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Spentdome, do me a favour, try this before any more coding:

    Turn your printer on, make sure it's plugged in. Go to a DOS prompt, change directory to where your source file is, and type the following:

    Code:
    type code3.c >LPT1
    Now, does this print anything? On my PC, my printer isn't connected via LPT1, so I get the following message:

    Code:
    C:\>type junk1.c >lpt1
    
    Write fault error writing device LPT1
    Abort, Retry, Ignore, Fail?
    We need to do this to proove that you can get to your printer via DOS. It's no good in printing something through Windows (say notepad or Word etc), because they use a completely different method of communication.

    You may have already done this, but from your posts, it's not clear what stage we're at.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  9. #24
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > type code3.c >LPT1
    I tried that, and it doesn't work.
    Although, I don't have type.
    The only dos editor I have is EDIT ( Micro$oft ).
    It apparently doesn't have redirection.
    The world is waiting. I must leave you now.

  10. #25
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    Originally posted by Shadow
    > type code3.c >LPT1
    I tried that, and it doesn't work.
    Although, I don't have type.
    OK, try Salem's Print (see earlier post). It should do the same thing.

    Out of interest, what OS you have Shadow?
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  11. #26
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Hammer

    OK, try Salem's Print (see earlier post). It should do the same thing.

    Out of interest, what OS you have Shadow?

    Do you meen this?
    Code:
    D:\code\test>dir
     Volume in drive D is Apps
     Volume Serial Number is 0429-F1A4
    
     Directory of D:\code\test
    
    27/05/2002  19:35       <DIR>          .
    27/05/2002  19:35       <DIR>          ..
                   0 File(s)              0 bytes
                   2 Dir(s)   1,974,091,776 bytes free
    What's this?

  12. #27
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    this:
    Code:
    D:\code>print /?
    Prints a text file.
    
    PRINT [/D:device] [[drive:][path]filename[...]]
    
       /D:device   Specifies a print device.
    I don't have the Print command, but you might.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  13. #28
    Registered User
    Join Date
    Apr 2002
    Posts
    41
    Originally posted by Hammer
    this:
    Code:
    D:\code>print /?
    Prints a text file.
    
    PRINT [/D:device] [[drive:][path]filename[...]]
    
       /D:device   Specifies a print device.
    I don't have the Print command, but you might.

    I don't understand you want me to do with this code?

  14. #29
    Unleashed
    Join Date
    Sep 2001
    Posts
    1,765
    > Out of interest, what OS you have Shadow?
    Win98SE.
    Oh, and I don't really need Salem's example.
    The program I posted works perfectly fine for me.
    The world is waiting. I must leave you now.

  15. #30
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    No, it's a DOS command. Print and Type are DOS commands. See if you have either, and see if you can get either of them to print anything. We're just trying to ensure DOS can talk directly to your printer, before you waste any more time doing coding.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Page 2 of 3 FirstFirst 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Set Printer Prior To Loading Print Dialog Box
    By Beaner in forum Windows Programming
    Replies: 3
    Last Post: 10-10-2008, 01:02 PM
  2. changing property (layout) of a printer
    By leojose in forum Windows Programming
    Replies: 5
    Last Post: 12-05-2005, 06:16 AM
  3. Sending window to printer
    By Bazzz in forum C++ Programming
    Replies: 3
    Last Post: 07-30-2004, 09:49 AM
  4. Success - Output to Printer
    By noraa in forum C Programming
    Replies: 5
    Last Post: 08-04-2002, 09:12 AM
  5. sending data to printer port
    By lliero in forum C Programming
    Replies: 3
    Last Post: 11-20-2001, 03:57 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21