Path: utzoo!utgpu!jarvis.csri.toronto.edu!clyde.concordia.ca!uunet!aplcen!uakari.primate.wisc.edu!zaphod.mps.ohio-state.edu!tut.cis.ohio-state.edu!ucbvax!hplabs!hpfcso!hpfcdc!rer From: rer@hpfcdc.HP.COM (Rob Robason) Newsgroups: comp.sys.hp Subject: Re: fopen/fseek interaction Message-ID: <5570351@hpfcdc.HP.COM> Date: 27 Dec 89 17:17:54 GMT References: Organization: HP Ft. Collins, Co. Lines: 58 > In porting a program from a BSD system to hp-ux version 3.10 on a 9k/800 > I discovered a difference in stdio behavior. > fd = fopen(path, "a") > is used on the BSD system to open, creating if necessary a file. Here are the open(2) flags for each mode: MODE BSD HP-UX ----------------------------------------------------------------------------- "a" O_CREAT | O_WRONLY* O_CREAT | O_APPEND | O_WRONLY* "r" O_RDONLY* O_RDONLY* "w" O_TRUNC | O_CREAT | O_WRONLY* O_TRUNC | O_CREAT | O_WRONLY* -------- * if the "+" is present in mode, uses O_RDWR The HP-UX code derives from SysV.2. I suspect the difference results from the earlier heritage of BSD from version 7, although I can't easily verify that. > On the hp-ux system the fseek's are effectively no-ops, that is the file > consists of as many numbers as there are writes. This is consistent with the O_APPEND flag to open(2). > Changing the "a" to a "r+" in the fopen works fine if the file exists, > but won't create a new file. At least this is consistent between the two systems. > "a+" seems to behave the same as "a". In all cases the fseek returns a > 0, indicating success. The fseek _does_ succeed, but the file pointer is repositioned at the time of the write by the OS. > A workaround is easy enough to implement, but it is annoying to have to > #ifdef the code. As Kathy mentions, the "w" mode will work and it is also the same on both systems. Just remember that it truncates the file, so if you're planning on reading the old number before writing a new one, you'll have a problem. If this is the case, you might consider splitting the create operation from the fopen so you can share code without ifdef. Whichever you use, this will be necessary in porting to other SysV (and as Kathy also mentions, POSIX) conformant systems as well. > I am not sure whether the difference is a bug in either the BSD or hp-ux > system, or some BSD/System V semantic difference. Does anyone know? From the description: "a" append: open for writing at end of file, or create for writing it sure seems like it was intended that O_APPEND was to be used. > Tom Arons Internet: arons@iris.ucdavis.edu Rob Robason