Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Strange Behaviour in finding Size of a File

Reply
Thread Tools

Strange Behaviour in finding Size of a File

 
 
Alain Ketterlin
Guest
Posts: n/a
 
      11-09-2012
Keith Thompson <kst-> writes:

> Alain Ketterlin <> writes:
>> felix <> writes:
>>> if ( stat ( logFile, &results ) == 0 )

>>
>> Use ftell() instead (or lseek() at level 2).

>
> What makes you think that will yield better results,


What makes you think it will not. At least ftell() is C.

> and what is "level 2"?


Unixism. Just ignore it if it hurts you.

-- Alain.
 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      11-09-2012
Alain Ketterlin <> writes:
> Keith Thompson <kst-> writes:
>> Alain Ketterlin <> writes:
>>> felix <> writes:
>>>> if ( stat ( logFile, &results ) == 0 )
>>>
>>> Use ftell() instead (or lseek() at level 2).

>>
>> What makes you think that will yield better results,

>
> What makes you think it will not. At least ftell() is C.


We know that stat() isn't working as the OP expects. I'd expect
ftell() to yield exactly the same results -- and it requires opening
the file and seeking to the end of it. Furthermore, there's no
guarantee in C that the fseek()/ftell() trick will accurately yield
the size of a file. Binary streams may legally be padded with an
implementation-defined number of null characters (N1370 7.21.2p3),
and "A binary stream need not meaningfully support fseek calls
with a whence value of SEEK_END" (7.21.9.2p3). For text streams,
the value returned by ftell() isn't necessarily meaningful except
as an argument to fseek() (7.21.9.4p2).

A POSIX environment makes more guarantees -- but as long as you're
depending on POSIX, there's no good reason not to use stat()
(or fstat() or lstat()).

My point is that you suggested ftell() as a solution to the OP's
problem. It isn't.

>> and what is "level 2"?

>
> Unixism. Just ignore it if it hurts you.


If you're referring to section 2 of the manual (ftell(2),
documentation available via "man 2 ftell"), I've never heard of
that being referred to as "level 2". Try being a little less
condescending and actually answering the question.

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
 
 
 
Alain Ketterlin
Guest
Posts: n/a
 
      11-10-2012
Keith Thompson <kst-> writes:

> Alain Ketterlin <> writes:
>> Keith Thompson <kst-> writes:
>>> Alain Ketterlin <> writes:
>>>> felix <> writes:
>>>>> if ( stat ( logFile, &results ) == 0 )
>>>>
>>>> Use ftell() instead (or lseek() at level 2).
>>>
>>> What makes you think that will yield better results,

>>
>> What makes you think it will not. At least ftell() is C.

>
> We know that stat() isn't working as the OP expects. I'd expect
> ftell() to yield exactly the same results -- and it requires opening
> the file and seeking to the end of it.


If stat() doesn't give the correct result in the OP's use case (writing
chunks, and testing whether the size has reached a limit), it's probably
because the file is still open. And several people have suggested that
the problem was probably with the buffering. The doc of ftell() says (on
my system):

| The ftell() function obtains the current value of the file position
| indicator for the stream pointed to by stream.

In my opinion, that's a pretty good hint given the problem description.

> Furthermore, there's no guarantee in C that the fseek()/ftell() trick
> will accurately yield the size of a file. Binary streams may legally
> be padded with an implementation-defined number of null characters
> (N1370 7.21.2p3), and "A binary stream need not meaningfully support
> fseek calls with a whence value of SEEK_END" (7.21.9.2p3). For text
> streams, the value returned by ftell() isn't necessarily meaningful
> except as an argument to fseek() (7.21.9.4p2).


I know all this, thank you, but I have no indication that the OP is in
any of these cases. I just gave the OP a track to follow. He/she would
surely come back with another (and maybe more precise) question if
things turn out to be more difficult.

> A POSIX environment makes more guarantees -- but as long as you're
> depending on POSIX, there's no good reason not to use stat()
> (or fstat() or lstat()).


Keeping the file open is a good reason to not use stat() (see code
above).

> My point is that you suggested ftell() as a solution to the OP's
> problem. It isn't.


OK, call it a hint if you want...

>>> and what is "level 2"?

>>
>> Unixism. Just ignore it if it hurts you.

>
> If you're referring to section 2 of the manual (ftell(2),
> documentation available via "man 2 ftell"), I've never heard of
> that being referred to as "level 2".


OK now you have. I didn't think it was that hard to understand, given
that lseek was two words away.

> Try being a little less condescending and actually answering the
> question.


Try being a little less condescending and actually helping people asking
for help instead of being pedantic (your words) on size units and
everything but the OP's problem.

-- Alain.
 
Reply With Quote
 
Philip Lantz
Guest
Posts: n/a
 
      11-10-2012
Alain Ketterlin wrote:
> Keith Thompson <kst-> writes:
> > Alain Ketterlin <> writes:
> >> Keith Thompson <kst-> writes:
> >>> Alain Ketterlin <> writes:
> >>>> felix <> writes:
> >>>>> if ( stat ( logFile, &results ) == 0 )
> >>>>
> >>>> Use ftell() instead (or lseek() at level 2).
> >>> and what is "level 2"?
> >>
> >> Unixism. Just ignore it if it hurts you.

> >
> > If you're referring to section 2 of the manual (ftell(2),
> > documentation available via "man 2 ftell"), I've never heard of
> > that being referred to as "level 2".

>
> OK now you have. I didn't think it was that hard to understand, given
> that lseek was two words away.


I didn't understand it either--I thought you meant to use a value of 2
for whence, and I thought it was a strange way to express that. It never
occurred to me you were referring to section 2 of the Unix Programmer's
Manual.
 
Reply With Quote
 
Kenny McCormack
Guest
Posts: n/a
 
      11-10-2012
In article <>,
Philip Lantz <> wrote:
....
>I didn't understand it either--I thought you meant to use a value of 2
>for whence, and I thought it was a strange way to express that. It never
>occurred to me you were referring to section 2 of the Unix Programmer's
>Manual.


Well, live & learn!

See, the Usenet can be a helpful thing after all.

--
Just for a change of pace, this sig is *not* an obscure reference to
comp.lang.c...

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-10-2012
On 11/09/2012 03:33 PM, Keith Thompson wrote:
> Alain Ketterlin <> writes:
>> Keith Thompson <kst-> writes:
>>> Alain Ketterlin <> writes:
>>>> felix <> writes:
>>>>> if ( stat ( logFile, &results ) == 0 )
>>>>
>>>> Use ftell() instead (or lseek() at level 2).
>>>
>>> What makes you think that will yield better results,

>>
>> What makes you think it will not. At least ftell() is C.

>
> We know that stat() isn't working as the OP expects. I'd expect
> ftell() to yield exactly the same results -- and it requires opening
> the file and seeking to the end of it. ...


Opening the file and seeking to the end is not a problem; it's already
open, or at least it was at the time of the last write, and the current
write position was presumably at the end of the file, otherwise it
wouldn't be growing. I'd expect ftell() to give a better indication of
the bytes written than stat()=>st_size, since I wouldn't expect the
value returned by ftell() to be affected by issues such as buffering.
--
James Kuyper
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-10-2012
James Kuyper <> writes:
> On 11/09/2012 03:33 PM, Keith Thompson wrote:
>> Alain Ketterlin <> writes:
>>> Keith Thompson <kst-> writes:
>>>> Alain Ketterlin <> writes:
>>>>> felix <> writes:
>>>>>> if ( stat ( logFile, &results ) == 0 )
>>>>>
>>>>> Use ftell() instead (or lseek() at level 2).
>>>>
>>>> What makes you think that will yield better results,
>>>
>>> What makes you think it will not. At least ftell() is C.

>>
>> We know that stat() isn't working as the OP expects. I'd expect
>> ftell() to yield exactly the same results -- and it requires opening
>> the file and seeking to the end of it. ...

>
> Opening the file and seeking to the end is not a problem; it's already
> open, or at least it was at the time of the last write, and the current
> write position was presumably at the end of the file, otherwise it
> wouldn't be growing. I'd expect ftell() to give a better indication of
> the bytes written than stat()=>st_size, since I wouldn't expect the
> value returned by ftell() to be affected by issues such as buffering.


Ok, good point.

That's assuming that the program that's querying the size of the file
is the same one that's writing to the file, and that the querying
code has access to the relevant FILE*. That's not entirely obvious
from the original post, but it seems likely.

(As I said earlier, C doesn't guarantee that the value returned by
ftell() is meaningful for text streams, but that's unlikely to be
an issue for the OP.)

--
Keith Thompson (The_Other_Keith) kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-10-2012
On 11/09/2012 10:24 PM, Keith Thompson wrote:
> James Kuyper <> writes:

....
>> Opening the file and seeking to the end is not a problem; it's already
>> open, or at least it was at the time of the last write, and the current
>> write position was presumably at the end of the file, otherwise it
>> wouldn't be growing. I'd expect ftell() to give a better indication of
>> the bytes written than stat()=>st_size, since I wouldn't expect the
>> value returned by ftell() to be affected by issues such as buffering.

>
> Ok, good point.
>
> That's assuming that the program that's querying the size of the file
> is the same one that's writing to the file, and that the querying
> code has access to the relevant FILE*. That's not entirely obvious
> from the original post, but it seems likely.


The behavior that's controlled by the results of this query is the
creation of a new log file, so it never even occurred to me to consider
that the log file might be being written by some other program.
--
James Kuyper
 
Reply With Quote
 
Philip Lantz
Guest
Posts: n/a
 
      11-10-2012
Kenny McCormack wrote:
>
> In article <>,
> Philip Lantz <> wrote:
> ...
> >I didn't understand it either--I thought you meant to use a value of 2
> >for whence, and I thought it was a strange way to express that. It never
> >occurred to me you were referring to section 2 of the Unix Programmer's
> >Manual.

>
> Well, live & learn!
>
> See, the Usenet can be a helpful thing after all.


What am I supposed to have learned? Is the term "level" commonly used to
identify a section of the Unix Programmer's Manual?
 
Reply With Quote
 
Eric Sosman
Guest
Posts: n/a
 
      11-10-2012
On 11/9/2012 10:24 PM, Keith Thompson wrote:
>[...]
> (As I said earlier, C doesn't guarantee that the value returned by
> ftell() is meaningful for text streams, but that's unlikely to be
> an issue for the OP.)


If the all the logging happens in one place (or a small
number of nearby places), and if it all happens during one
execution of the program, the O.P. can do the entire job in
purely portable C. Something like

static FILE *logStream;
static size_t logLength;

void writeLog(const char *format, ...) {
if (logStream == NULL) {
logStream = openLog(...);
logLength = 0;
}

va_list ap;
va_start(ap, format);
logLength += vfprintf(format, ap);

va_end(ap);
if (logLength > LIMIT) {
closeLog(logStream);
logStream = NULL;
}
}

.... should do it, along with a little error-checking and such.

(Okay, okay: The number of characters written to a stream is
not necessarily the same thing as the number of bytes stored on
a disk. Nonetheless, for "start a new log when the old one gets
too big" purposes it should be close enough.)

--
Eric Sosman
d
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
A strange behaviour of a File property alelvb@inwind.it Java 38 11-16-2011 01:10 PM
Preferred Size, Minimum Size, Size Jason Cavett Java 5 05-25-2008 08:32 AM
mega pixels, file size, image size, and print size - Adobe Evangelists Frank ess Digital Photography 0 11-14-2006 05:08 PM
strange file upload behaviour (404 with one pdf but not with another) mikecom@gmx.net ASP .Net 3 06-27-2006 10:15 AM
Strange behaviour when parsing a XML file Francesco Moi Perl Misc 2 07-27-2005 04:47 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57