tech-userlevel archive

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index][Old Index]

Re: fopen(.., "a") + fseek

    Date:        Thu, 6 Nov 2008 11:06:38 -0500
    From:        "Martin S. Weber" <>
    Message-ID:  <20081106160638.GC741%agamemnon.entropie.local@localhost>

  | Nope. I've opened the file with "a" which is for writing only. In
  | this light, the fseek "fails".

No, the fseek() works just fine - while in just "a" mode you can't use
fread() (or any other input function) to check that, you can still use ftell().

However the fseek() doesn't fail, it is just mostly useless.   But being
useless isn't a reason for a function to return an error - you can't expect
the system, or libc, or other, functions to find every bug for you.

Or to take another example ...

        fd = fopen("/some/file", "r");

                /* whatever */

        if (fseek(fd, 100L, SEEK_SET) != 0)
                errx(1, "fseek1");
        if (fseek(fd, 0L, SEEK_END) != 0)
                errx(1, "fseek2");

                /* more stuff */


By your reasoning the first fseek() should return an error, as it "failed",
in that it has no useful effect.

Now of course, that's absurd - but it is also exactly what happens in the
"a" open case with a write - every write does a seek to the current end
immediately before writing (in a way that is not subject to race conditions,
but that isn't relevant here.)   Just as in my stupid code above, the first
fseek() is just a useless waste of time, so it is when you fseek() in "a"
open mode and then generate output.


Home | Main Index | Thread Index | Old Index