Article View: comp.os.msdos.programmer
Article #4098Re: Turbo C and ^Z's
From: Otto J. Makela
Date: Mon, 07 Jan 1991 06:08
Date: Mon, 07 Jan 1991 06:08
66 lines
2701 bytes
2701 bytes
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 <stdio.h>
#include <fcntl.h>
#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 <otto@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 * * * * * * * * * * * * * * * * */
Technical Details
Message-ID:
<OTTO.91Jan7070856@tukki.jyu.fi>Path:
arc1-ca.pugleaf.net!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!ottoReferences:
<OTTO.91Jan3125711@tukki.jyu.fi> <10251@hydra.Helsinki.FI> <OTTO.91Jan4115449@tukki.jyu.fi> <1991Jan04.173903.20643@dirtydog.ima.isc.com>Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!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: <OTTO.91Jan7070856@tukki.jyu.fi> Date: 7 Jan 91 06:08:56 GMT References: <OTTO.91Jan3125711@tukki.jyu.fi> <10251@hydra.Helsinki.FI> <OTTO.91Jan4115449@tukki.jyu.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