Turbo C and ^Z's

Otto J. Makela otto at tukki.jyu.fi
Thu Jan 3 22:57:11 AEST 1991


In a fit of desperation, I wrote the following code to get rid of trailing
SUB's (^Z's, 0x1A) from MeSsy-DOS files, to make porting programs from Unix
easier... what am I doing wrong ?  Or is it the compiler (I tried compiling
this with an ancient Turbo C 2.0) ?  I've made similar code to work on Manx
Aztec C86 4.10d (a nice compiler, but sadly out of date).
--
#include	<stdio.h>
#include	<fcntl.h>

/*
**	MeSsy-DOS kludge for fopen-append
*/
FILE *fopena(filename)
char *filename;	{
	FILE *f;

	if(f=fopen(filename,"a+b"))	{
		fseek(f,-1L,SEEK_END);
		while(fgetc(f)==0x1A) fseek(f,-2L,SEEK_CUR);
			/* This is needed to clear stdio buffering */
		fseek(f,ftell(f),SEEK_SET);
			/* Then set text mode on the stream */
		setmode(fileno(f),O_TEXT);
	}
	return f;
}

main(argc,argv)
int argc;
char *argv[];	{
	FILE *f;

	if(argc<2)
		fprintf(stderr,"usage: %s file_to_append_to\n",*argv);
	else if(f=fopena(argv[1]))	{
		fprintf(f,"This is the appended text.\n");
		fclose(f);
	} else
		fprintf(stderr,"%s: could not open %s for append\n",
			*argv,argv[1]);
}
--
   /* * * Otto J. Makela <otto at jyu.fi> * * * * * * * * * * * * * * * * * * */
  /* Phone: +358 41 613 847, BBS: +358 41 211 562 (CCITT, Bell 24/12/300) */
 /* Mail: Kauppakatu 1 B 18, SF-40100 Jyvaskyla, Finland, EUROPE         */
/* * * Computers Rule 01001111 01001011 * * * * * * * * * * * * * * * * */



More information about the Comp.lang.c mailing list