fseek/fgetc puzzle

Nathan Wagner nw at hydaspes.if.org
Mon Feb 20 22:38:19 AEDT 2023


On Mon, Feb 20, 2023 at 10:21:31PM +1100, Darren Tucker wrote:

> > My suggestion of course fails to explain why there's only one 'B'.
> > I would create the initial file separately from the test program.
> 
> The "od" call in the middle of the above example proves there's only
> one byte after the first fclose.

Ah, so it does.  What happens
if you do everything except the last call at the end?
printf("fputc B = %d\n", fputc('B', f));

What happens if you don't read the last byte?  I realize
you will need to for the original problem?

What I'm wondering is if somehow reading the byte puts it in
the output buffer.

What happens if you create an initial two byte file, say "AB", and try
to write a 'C'?  If you read the B, I guess we expect 'ABBC' at this
point.  If you read the 'A', do you get 'ABAC' or something else?

I wish I had a box I could reproduce the problem on (see below).

> You get the AAB on Linux and MacOS?  Which versions?  I get AB on both
> OSX High Sierra and Fedora 36.  Maybe it's sensitive to locale
> settings?

I mis-quoted.  I get the expected two byte 'AB' output on both.
Sorry for what must have been a surprising confusion.

-- 
nw


More information about the openssh-unix-dev mailing list