Send .txt-file to USB-printer using C-Code

Hello,

this is my first question in this forum, so please give my answer if you need more information abaout my system/configuration/...

I'm a total beginner in C-programming using a book to teach myself (http://www.erlenkoetter.de/html/c.htm, its the first book in german).

I'm using a laptop with windows vista, my printer is a Brother HL-2040 laser-printer which is using a USB to my laptop (only way to communicate with printer).

I want to program a simple C-Code which opens and reads a .txt-file line after line and send it to the printer.

My code is:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define SEITENLAENGE 65
#define SCHMAL "1B2873313648" /* PCL for special font */
#define NEUE_SEITE "0c" /* PCL for new site */
#define NORMAL "1B2873313248" /* PCL for normal font */
#define MAX_EINGABE 200
FILE *text;
void left_margin(int rand){
	while(rand-- >0)
		fprintf(text,"%c",32);
}
void kopfzeile(char *dateiname,int linker_rand){
	static int seite=1;
	fprintf(text,"\n");		/* printing 1 empty line before starting */
	left_margin(linker_rand);
	fprintf(text,"<<< Seite %3i >>>		von %s\n\n",seite++,dateiname);
}
void steuerzeichen(char *seq){
	char wert;
	while(*seq){
		sscanf(seq,"%2x",&wert);		/* read the hole sequenz */
		fprintf(text,"%c",wert);		/* take 2 signs at ones */
		seq+=2;
	}
}

main(){
	FILE *stream;
	char dateiname[67];		     /* file to print */
	int zeile=0;					     /* actual print-line */
	int linker_rand=10;        /* left rand-space */
	char schmal='J';				   /* optional font J/N */
	char eingabe[MAX_EINGABE]; /* "enter"-dialog */
	printf("\n\t\t<<< formated print >>>");
	printf("\n\n<RETURN> ends the program.\n");
	/*----- read file and open it */
	printf("which file to print?\n>");
	gets(dateiname);
	if(strlen(dateiname)==0)		/* just <ENTER> ends the program here  */
		exit(0);
	while((stream=fopen(dateiname,"r"))==NULL){
		printf("'%s' not found!\a\n",dateiname);
		printf("\nwhich file to print?\n>");
		gets(dateiname);
		if(strlen(dateiname)==0)
			exit(0);
	}
	/*----- getting printer parameter */
	...
	/*----- open printer */
	text=fopen("LPT1","a");
	if(text==NULL){
		printf("\nerror opening LPT1");
		exit(1);
	}
	/*----- setting font */
...
	/*----- printing the list */
	kopfzeile(dateiname,linker_rand);
	while(fgets(eingabe,MAX_EINGABE,stream)!=NULL){
		left_margin(linker_rand);
		fprintf(text,"%s",eingabe);
		if(++zeile>SEITENLAENGE-10){
			steuerzeichen(NEUE_SEITE);
			kopfzeile(dateiname,linker_rand);
			zeile=0;
		}
	}
	steuerzeichen(NEUE_SEITE);
...


The program stops with the message "printf("\nerror opening LPT1");"

I've searched for the problem and have tried:

1: to go into my printer-configuration and making a printer-pool with my printer as USBxxx and LPT1 -->doesnt work

2: to go into my printer-configuration and disable the spooler, choosing "sending prints directly to the printer" (this is my translation of the german text!)--> doesnt work

3: try to search out whether my #define statements defining my PCLs are correct, but I couldnt find out where to look so i couldnt say if these are not correct

4: change between "LPT1" and "LPT1:"-->doesnt make any difference

Further more, I've read articles about giving my printer free in a network (didnt understand it ??) and others saying something about API, but it sounded bit more difficult for an beginner so i stopped it (not that i've understand it^^).

I'd be thankful for answers and solutions

steve
Last edited on
LPT1: is the parallel port. Your printer is NOT connected to the parallel port, if any. It is connected to a USB port.

You need to open the USB port, if this way of printing is accepted by your printer port.

I cannot help further because I don't know how to implement my own suggestion. :-D Good luck!
Oh, and Windows provides a standard way of printing that works on every installed printer. You should not be doing this kind of stuff anyway.

You should get a device context for the printer and draw the contents of the file into the device context. This way you'll get your printer to print, and pretty much any other installed printer connected via any other method (like the network).
Topic archived. No new replies allowed.