Turbo C, fopen(), fprintf() and ^Z - I need help!!

Tom Almy toma at tekgvs.LABS.TEK.COM
Thu Sep 20 01:23:19 AEST 1990


In article <38742 at ucbvax.BERKELEY.EDU> brand at janus.Berkeley.EDU writes:
>I should like some help with a simple problem in Turbo C. I have a
>C program which I have been running on UNIX with no problem. I am
>trying to port it to Turbo C 2.0 running on my '386 under MSDOS (V3.3)
>but am having a strange problem. The program opens files for appending
>data using the:

I'm sure you mean fp=fopen(filename,"a")

>	fopen(fp,filename,"a")
>construction and writes the data with the usual
>	fprintf(fp,"%s ...%d\n",.....)
>instruction. The problem that I am having is that the line that is 
>appended in the file is preceeded with a ^Z which makes the data
>invisible when a type command is issued from MSDOS.

The file undoubtedly had the ^Z character at the end when you started.
You can't delete it when opening in "a" mode because this mode forces all
writes to occur at the end of file. You will need to do something like this:

if (fp = fopen(filename,"r+") { /* file exists */
	fseek(fp,-1L,SEEK_END);	/* check last character */
	if (fgetc(fp) == 26) /* last character is a control-Z, delete it */
		fseek(fp,-1L,SEEK_END);
}
else { /* file does not already exist */
	if ((fp = fopen(filename,"w")) == NULL) {
		/* Error code for unable to create file */
	}
}


Tom Almy
toma at tekgvs.labs.tek.com
Standard Disclaimers Apply


	



More information about the Comp.lang.c mailing list