[En-Nut-Discussion] Fmode

Ben Hoyt benhoyt at gmail.com
Fri Sep 8 12:12:12 CEST 2006


Yep, I'm almost certain that fmode() behaviour is wrong. It especially
doesn't make sense for "a+", because that's append, and you can't append if
you've truncated. "+" just means "update" or "read and write":

http://www.acm.uiuc.edu/webmonkeys/book/c_guide/2.12.html#fopen

And here's what K&R (ANSI version) says:

FILE *fopen(const char *filename, const char *mode) fopen opens the named
file, and returns a stream, or NULL if the attempt fails. Legal values for
mode include:

  "r" open text file for reading "w" create text file for writing; discard
previous contents if any "a" append; open or create text file for writing at
end of file "r+" open text file for update (i.e., reading and writing)
"w+" create
text file for update, discard previous contents if any "a+" append; open or
create text file for update, writing at end
Update mode permits reading and writing the same file; fflush or a
file-positioning function must be called between a read and a write or vice
versa. If the mode includes b after the initial letter, as in "rb" or "w+b",
that indicates a binary file. Filenames are limited to
FILENAME_MAXcharacters. At most
FOPEN_MAX files may be open at once. Cheers,
Ben


---------- Forwarded message ----------
> From: Harald Kipp <harald.kipp at egnite.de>
> To: "Ethernut User Chat (English)" <en-nut-discussion at egnite.de>
> Date: Thu, 07 Sep 2006 09:54:21 +0200
> Subject: Re: [En-Nut-Discussion] Fmode
> Hello Ben,
>
> At 16:11 06.09.2006 +1200, you wrote:
> >Hi guys,
> >
> >I've just been look at _fmode() in crt/fmode.c, and I can't figure out
> why,
> >between file versions 1.1.1.1 and 1.2, the behaviour of "+" was changed
> from
> >just adding read/write access, to adding the CREAT and TRUNC flags. I'm
> >pretty sure it should just add RDWR. This is a bug, right?
> >
> >1.1.1.1: mflags |= _O_RDWR;
> >1.2: mflags |= _O_RDWR | _O_CREAT | _O_TRUNC;
>
> I changed that. When using fopen(..., "r+"), I expect an existing
> file being truncated or a non-existing file being created.
>
> Am I wrong here?
>
> Harald
>

-- 
Ben Hoyt
Mobile: +64 21 331 841
Email: benhoyt at gmail.com



More information about the En-Nut-Discussion mailing list