For Programmers: Free Programming Magazines  


Home > Archive > Unix Programming > March 2005 > fopen(file, "a+") and stream position









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author fopen(file, "a+") and stream position
Andrei Voropaev

2005-03-23, 3:58 pm

Sorry if this question is already in the FAQ somewhere. Please direct me
there in this case.

Reading man fopen

a+ Open for reading and appending (writing at end of file). The file is cre-
ated if it does not exist. The stream is positioned at the end of the file.

So, I believe the stream should be at the end of file if I open it with
"a+" mode. But in reality the commands

fs = fopen(file, "a+");
printf("File position: %d\n", (int) ftell(fs));

Result in

File position: 0

So, how should I understand the manual?

--
Minds, like parachutes, function best when open
Måns Rullgård

2005-03-23, 3:58 pm

Andrei Voropaev <avorop@mail.ru> writes:

> Sorry if this question is already in the FAQ somewhere. Please direct me
> there in this case.
>
> Reading man fopen
>
> a+ Open for reading and appending (writing at end of file).
> The file is cre- ated if it does not exist. The stream is
> positioned at the end of the file.
>
> So, I believe the stream should be at the end of file if I open it with
> "a+" mode. But in reality the commands
>
> fs = fopen(file, "a+");
> printf("File position: %d\n", (int) ftell(fs));
>
> Result in
>
> File position: 0
>
> So, how should I understand the manual?


You should read the info pages as well:

`a+'
Open or create file for both reading and appending. If the
file exists, its initial contents are unchanged. Otherwise,
a new file is created. The initial file position for reading
is at the beginning of the file, but output is always
appended to the end of the file.

--
Måns Rullgård
mru@inprovide.com
Andrei Voropaev

2005-03-23, 3:58 pm

On 2005-03-23, Måns Rullgård <mru@inprovide.com> wrote:
> Andrei Voropaev <avorop@mail.ru> writes:
>
>
> You should read the info pages as well:
>
> `a+'
> Open or create file for both reading and appending. If the
> file exists, its initial contents are unchanged. Otherwise,
> a new file is created. The initial file position for reading
> is at the beginning of the file, but output is always
> appended to the end of the file.
>


Ok. Thank you. Looks like I can't trust to man pages on Linux. Though
I'm still not sure if I can trust info pages as well. At least clearerr
mentions that EOF can be cleared "also" by file positioning functions,
while in fact, at least if the file is opened in "a+" mode, then fread
automaticaly clears EOF if the file has grown :) NetBSD strictly follows
the man :)


--
Minds, like parachutes, function best when open
Måns Rullgård

2005-03-23, 3:58 pm

Andrei Voropaev <avorop@mail.ru> writes:

> On 2005-03-23, Måns Rullgård <mru@inprovide.com> wrote:
>
> Ok. Thank you. Looks like I can't trust to man pages on Linux. Though
> I'm still not sure if I can trust info pages as well. At least clearerr
> mentions that EOF can be cleared "also" by file positioning functions,
> while in fact, at least if the file is opened in "a+" mode, then fread
> automaticaly clears EOF if the file has grown :) NetBSD strictly follows
> the man :)


SuS says nothing about the initial read position for file opened in a+
mode. An explicit fs() is probably the safe way to go.

--
Måns Rullgård
mru@inprovide.com
Geoff Clare

2005-03-24, 4:00 pm

Måns Rullgård <mru@inprovide.com> wrote, on Wed, 23 Mar 2005:

> SuS says nothing about the initial read position for file opened in a+
> mode.


Nit pick: it's not true that it says "nothing". It explicitly
allows the initial position to be either at the beginning or the end.
(See the first paragraph of section 2.5 Standard I/O Streams.)

> An explicit fs() is probably the safe way to go.


True.

--
Geoff Clare <netnews@gclare.org.uk>

Måns Rullgård

2005-03-24, 4:00 pm

Geoff Clare <geoff@clare.See-My-Signature.invalid> writes:

> Måns Rullgård <mru@inprovide.com> wrote, on Wed, 23 Mar 2005:
>
>
> Nit pick: it's not true that it says "nothing". It explicitly
> allows the initial position to be either at the beginning or the end.
> (See the first paragraph of section 2.5 Standard I/O Streams.)


You're right. I looked at the description of fopen, and didn't find
anything there.

--
Måns Rullgård
mru@inprovide.com
Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2010 codecomments.com