tobold.org

correct • elegant • free

△ Comp.lang.c △

◅ Where would I use ^ ?

Key Sort ▻

File truncation

In article <3sphu6$i1r@netnews.upenn.edu>,
Jeffrey M. Perkel <perkel@dolphin.upenn.edu> wrote:
>   5.  force end of file with fputc (EOF, fp);
> ...
>How come my UNIX compiler doesn't recognize EOF and force the end of the
>file?

Because there is no EOF character in Unix.  The operating system "just
knows" how big each file is, and indicates end of file to a process by
returning 0 (i.e. no bytes read) from the read(2) system call.

Library routines such as getchar(3) notice when read(2) returns 0, and
return EOF.  EOF is not a character which can ever appear in a file;
this is why getchar(3) and friends must have return type int, rather
than char.

>       That is, I want to truncate the file at the appropriate length?

There is no way to truncate a file in ISO C.

Most versions of Unix implement the truncate(2) and ftruncate(2) calls,
which arbitrarily set the length of a file.  All versions of Unix
provide a way to truncate a file to 0 bytes: either the O_TRUNC flag to
open(2), or creat(2).

Followups to comp.unix.programmer.

Tim.
--
Tim Goodwin        | "If you're used to paint-by-number, a blank canvas
Public IP Exchange | can be unsettling at first." -- Larry Wall

Original headers:

From: tim@pipex.net (Tim Goodwin)
Newsgroups: comp.lang.c,comp.unix.programmer
Subject: Re: File truncation
Followup-To: comp.unix.programmer
Date: 28 Jun 1995 14:55:15 +0100
Organization: PIPEX, 216 Science Park, Cambridge, England
Message-ID: <3srn03$1kq@pipe.pipex.net>
References: <3sphu6$i1r@netnews.upenn.edu>

△ Comp.lang.c △

◅ Where would I use ^ ?

Key Sort ▻