Xref: utzoo comp.lang.c:31967 comp.sys.ibm.pc.misc:1829 Path: utzoo!attcan!uunet!zephyr.ens.tek.com!tekcrl!tekgvs!toma From: toma@tekgvs.LABS.TEK.COM (Tom Almy) Newsgroups: comp.lang.c,comp.sys.ibm.pc.misc Subject: Re: Turbo C, fopen(), fprintf() and ^Z - I need help!! Message-ID: <8141@tekgvs.LABS.TEK.COM> Date: 19 Sep 90 15:23:19 GMT References: <38742@ucbvax.BERKELEY.EDU> Reply-To: toma@tekgvs.LABS.TEK.COM (Tom Almy) Followup-To: comp.lang.c Organization: Tektronix, Inc., Beaverton, OR. Lines: 38 In article <38742@ucbvax.BERKELEY.EDU> brand@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@tekgvs.labs.tek.com Standard Disclaimers Apply