Path: csiph.com!eternal-september.org!feeder.eternal-september.org!mx02.eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: How to read a data file whose size is unknown in C? Date: Thu, 13 Aug 2015 09:14:57 -0700 Organization: None to speak of Lines: 44 Message-ID: References: <7b7a215a-b72a-48ba-bbdc-ddc22ad8c5b7@googlegroups.com> <87oaib4bdo.fsf@bsb.me.uk> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit Injection-Info: mx02.eternal-september.org; posting-host="945944de09706c9b4e29b53c9d2efdc2"; logging-data="7139"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX19UF5Rybmd02tMcIDLCF1pu" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/23.1 (gnu/linux) Cancel-Lock: sha1:WTjLdIr2Xy5+pjBzdBcsD3/CVD4= sha1:+6Ik1uhLnxaOh6j2uOHR0vtEGKc= Xref: csiph.com comp.lang.c:67303 jacobnavia writes: > Le 13/08/2015 14:32, Ben Bacarisse a écrit : [...] >> But it fails on most (all?) of those systems if the file is a pipe or >> some other kinds of special files. It's important to point that out >> because it can seriously harm the usefulness of a tool if can only work >> with seekable files. It's probably not important to the OP, but anyone >> using fseek to get a file's size should know all the consequences of >> that design. >> > > Well, that's true. > > So, the universally accepted advice: > > "Just read all the file, then you know its size" is ALSO wrong!!! > > You just keep reading the pipe and then... well, you can't start from > scratch! Who says you need to start from scratch? You can read from a pipe until you get an end-of-file indication. With judicious use of realloc(), you can store all the data from the pipe in an allocated array. You *can't* do the same thing by using fstat(), or fseek/ftell. If the pipe never reports end-of-file, hopefully you'll have some way to handle that. Similar considerations apply to other things that act like files but are not ordinary fixed-size disk files. For example, I can read 1898 bytes from /proc/stat on my system, but the file system reports its size as 0 (and fseek() appears to work without error, but ftell() then returns 0, which is odd). I can type "view /proc/stat" ("view" is a read-only version of the vi/vim text editor), and it works. Apparently vim is clever enough not to assume that either fstat or fseek/ftell reports an accurate size. -- Keith Thompson (The_Other_Keith) kst-u@mib.org Working, but not speaking, for JetHead Development, Inc. "We must do something. This is something. Therefore, we must do this." -- Antony Jay and Jonathan Lynn, "Yes Minister"