[comp.lang.c] HELP with stdprn in BINARY

brianh@hpcvia.CV.HP.COM (brian_helterline) (12/14/89)

I have a problem using stdprn in binary mode with MSC 5.1  I set
everything up and start printing the file, but the fwrite ends
up bombing out.  Is the code wrong?  Has anyone experienced this?
I changed to using _bios_printer() and it worked fine.  If I print
to a file rather than stdprn, the program works also.  WHY??????

Any comments are welcome.
AdvTHANKSance

Brian

/******************************************************************/
/* MS-C 5.1 */
#include <stdio.h>
#include <stdlib.h>
#include <fcntl.h>
#include <io.h>

#define BUF_SIZE		8196

char ReadBuf[BUF_SIZE];


int main(int argc, char **argv)
{
	FILE *File;
	int n, nbytes;

	/*
	 * Set stdprn to binary to avoid unwanted LF->CR-LF translation
	 */
	n = setmode(fileno(stdprn),O_BINARY);
	if( n == -1 )
	{
		fprintf( stderr, "Can't change mode of stdprn\n" );
		exit( 1 );
	}

	File = fopen(argv[1], "rb");
	if( File == NULL )
	{
	    fprintf( stderr, "Can't open %s\n", argv[1] );
	    exit( 1 );
	}

	printf("Downloading\n");

	while( (n=fread(ReadBuf, sizeof(char), BUF_SIZE, File)) == BUF_SIZE )
	{
	    nbytes = fwrite(ReadBuf, sizeof(char), BUF_SIZE, stdprn );
	    if( nbytes != BUF_SIZE )
	    {
		fprintf(stderr,"Write failure, %d != %d\n", nbytes, BUF_SIZE);
		exit( 1 );
	    }
	}
	if( ferror( File ) )
	{
		fprintf( stderr, "Error occured while downloading");
		exit( 1 );
	}
	else
	{
		nbytes = fwrite(ReadBuf, sizeof(char), n, stdprn );
		if( nbytes != n )
		{
		    fprintf( stderr, "Write failure, %d != %d\n", nbytes, n );
		    exit( 1 );
		}
	}
		
	(void) fclose( File );
	exit(0);
}