Xref: utzoo comp.os.msdos.programmer:2696 comp.lang.c:35071 Path: utzoo!utgpu!news-server.csri.toronto.edu!cs.utexas.edu!sdd.hp.com!wuarchive!zaphod.mps.ohio-state.edu!rpi!uupsi!sunic!news.funet.fi!tukki.jyu.fi!jyu.fi!otto From: otto@tukki.jyu.fi (Otto J. Makela) Newsgroups: comp.os.msdos.programmer,comp.lang.c Subject: Re: Turbo C and ^Z's Message-ID: Date: 7 Jan 91 06:08:56 GMT References: <10251@hydra.Helsinki.FI> <1991Jan04.173903.20643@dirtydog.ima.isc.com> Sender: news@tukki.jyu.fi (News articles) Organization: Turing Police, Criminal AI section Lines: 66 In-Reply-To: karl@ima.isc.com's message of 4 Jan 91 17:39:03 GMT Nntp-Posting-Host: jyu.fi Followups-To: comp.os.msdos.programmer In article <1991Jan04.173903.20643@dirtydog.ima.isc.com> karl@ima.isc.com (Karl Heuer) writes: [...] Lars is correct. On a file opened in append-mode, fseek() is only useful for reading. (Though some historical implementations implemented append- mode with an initial seek-to-end only, this is not acceptable in ANSI C.) Yes. Confirm. Affirmative. Now that I've gone thru it, this seems to be how it works. I didn't realize ANSI decided to break compatibility with Unix in such a radical way... >I'd like to point out I can't use "r+b", 'cause it'll fail if the file does >not exist beforehand. Then use "w+b". Can't do that either, 'cause I don't want to kill the file. I need both. Here is the fixed code, if someone finds it useful... -- #include #include #ifdef UNIX #define fopena(filename) fopen(filename,"a") #else /* ** MeSsy-DOS kludge to replace fopen(filename,"at") */ FILE *fopena(filename) char *filename; { FILE *f; if(f=fopen(filename,"r+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); setmode(fileno(f),O_TEXT); } else f=fopen(filename,"wt"); return f; } #endif /* ** Test main program */ 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 * * * * * * * * * * * * * * * * * * */ /* 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 * * * * * * * * * * * * * * * * */