Account Options

  1. Sign in
The old Google Groups will be going away soon, but your browser is incompatible with the new version.
Google Groups Home
« Groups Home
Strange Behaviour in finding Size of a File
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  Messages 1 - 25 of 36 - Collapse all  -  Translate all to Translated (View all originals)   Newer >
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Followup To:
Add Cc | Add Followup-to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers you hear
 
felix  
View profile  
 More options Nov 9 2012, 2:09 am
Newsgroups: comp.lang.c
From: felix <ckpra...@gmail.com>
Date: Thu, 8 Nov 2012 23:09:49 -0800 (PST)
Local: Fri, Nov 9 2012 2:09 am
Subject: Strange Behaviour in finding Size of a File
This method was written to create new Log File, when the size of the Log File reaches a max size defined by user [10MB in our case]. Here is the code snippet that does this check:

//-- Code starts here : --

static size_t LogSize = 1048576;
bool CreateNewLogs = false;

if ( stat ( logFile, &results ) == 0 )
{
        if ( results.st_size > LogSize )
        {
                CreateNewLogs = true;
        }

}

//-- Code ends here   : --

It is strange that the condition got satisfied when results.st_size = 2589116.
And we are sure that the size of the data that is written is between 50 to 100 bytes in one operation. And this check is done before writing into the LogFile.

I am not sure if I am missing anything in our understanding of the stat function. Any inputs or pointers on this regard will be really Helpful.

Thanks in advance, and please let me know if any other information is required.

--
Felix


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Nov 9 2012, 6:56 am
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Fri, 09 Nov 2012 06:56:26 -0500
Local: Fri, Nov 9 2012 6:56 am
Subject: Re: Strange Behaviour in finding Size of a File
On 11/09/2012 02:09 AM, felix wrote:

> This method was written to create new Log File, when the size of the Log File reaches a max size defined by user [10MB in our case]. Here is the code snippet that does this check:

> //-- Code starts here : --

> static size_t LogSize = 1048576;
> bool CreateNewLogs = false;

> if ( stat ( logFile, &results ) == 0 )

That is presumably the POSIX stat() function, or something similar? If
so, its behavior is defined by the POSIX standard, not the C standard,
and you'll get better answers to your questions in comp.unix.programmer
than in this newsgroup.

> {
>    if ( results.st_size > LogSize )
>    {
>            CreateNewLogs = true;
>    }
> }
> //-- Code ends here   : --

> It is strange that the condition got satisfied when results.st_size = 2589116.
> And we are sure that the size of the data that is written is between 50 to 100 bytes in one operation. And this check is done before writing into the LogFile.

Keep in mind that file I/O is normally buffered, so the buffer size is
more relevant than the size of your individual writes. Still, that seems
to be a rather large jump to explain by buffering.

> I am not sure if I am missing anything in our understanding of the stat function. Any inputs or pointers on this regard will be really Helpful.

The people in comp.unix.programming may need to know more details about
how data is written to the file, and whether or not you've used any
POSIX functions to change the file mode.
Just to get a better idea of what's going on, I'd recommend reporting
the file size somewhere (probably in a separate log file) every time you
call stat().
--
James Kuyper

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Nov 9 2012, 8:24 am
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@comcast-dot-net.invalid>
Date: Fri, 09 Nov 2012 08:24:46 -0500
Local: Fri, Nov 9 2012 8:24 am
Subject: Re: Strange Behaviour in finding Size of a File
On 11/9/2012 2:09 AM, felix wrote:

> This method was written to create new Log File, when the size of the Log File reaches a max size defined by user [10MB in our case]. Here is the code snippet that does this check:

> //-- Code starts here : --

> static size_t LogSize = 1048576;

     Ah. This is obviously some strange usage of "10 MB" that
I hadn't previously been aware of.

> It is strange that the condition got satisfied when results.st_size = 2589116.

     Not all *that* strange ...

--
Eric Sosman
esos...@comcast-dot-net.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
John Gordon  
View profile  
 More options Nov 9 2012, 10:16 am
Newsgroups: comp.lang.c
From: John Gordon <gor...@panix.com>
Date: Fri, 9 Nov 2012 15:16:48 +0000 (UTC)
Local: Fri, Nov 9 2012 10:16 am
Subject: Re: Strange Behaviour in finding Size of a File
In <8439d649-fae9-4828-8a86-d0c7c8489aa5@googlegroups.com> felix <ckpra...@gmail.com> writes:

> static size_t LogSize = 1048576;
>    if ( results.st_size > LogSize )
> It is strange that the condition got satisfied when
> results.st_size = 2589116.

You're surprised that 2589116 is greater than 1048576?

--
John Gordon                   A is for Amy, who fell down the stairs
gor...@panix.com              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Mark Bluemel  
View profile  
 More options Nov 9 2012, 10:56 am
Newsgroups: comp.lang.c
From: Mark Bluemel <mark_blue...@pobox.com>
Date: Fri, 09 Nov 2012 15:56:34 +0000
Local: Fri, Nov 9 2012 10:56 am
Subject: Re: Strange Behaviour in finding Size of a File
On 09/11/2012 15:16, John Gordon wrote:

> In <8439d649-fae9-4828-8a86-d0c7c8489aa5@googlegroups.com> felix <ckpra...@gmail.com> writes:

>> static size_t LogSize = 1048576;

>>        if ( results.st_size > LogSize )

>> It is strange that the condition got satisfied when
>> results.st_size = 2589116.

> You're surprised that 2589116 is greater than 1048576?

No. Given that "we are sure that the size of the data that is written is
between 50 to 100 bytes in one operation. And this check is done before
writing into the LogFile." I think the OP is surprised that the
condition wasn't satisfied earlier.

I think James Kuyper has given some good advice.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Nov 9 2012, 11:02 am
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Fri, 09 Nov 2012 11:01:59 -0500
Local: Fri, Nov 9 2012 11:01 am
Subject: Re: Strange Behaviour in finding Size of a File
On 11/09/2012 10:16 AM, John Gordon wrote:

> In <8439d649-fae9-4828-8a86-d0c7c8489aa5@googlegroups.com> felix <ckpra...@gmail.com> writes:

>> static size_t LogSize = 1048576;

>>        if ( results.st_size > LogSize )

>> It is strange that the condition got satisfied when
>> results.st_size = 2589116.

> You're surprised that 2589116 is greater than 1048576?

No, he's surprised that, when checking this condition periodically,
separated by writes of no more than 100 bytes, that it doesn't trigger
until 2589116. Naively, it could be expected to trigger with
results.st_size no more than 100 bytes larger than LogSize. Buffering is
the simplest of the many reasons invalidating that conclusion; there's
several others, and many people who are better equipped to explain those
issues than I am, so I'll leave that explanation to them, rather than
embarrassing myself by getting it wrong.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Greg Martin  
View profile  
 More options Nov 9 2012, 11:59 am
Newsgroups: comp.lang.c
From: Greg Martin <g...@softsprocket.com>
Date: Fri, 09 Nov 2012 08:59:20 -0800
Local: Fri, Nov 9 2012 11:59 am
Subject: Re: Strange Behaviour in finding Size of a File
On 12-11-08 11:09 PM, felix wrote:

I would think it strange if I knew for sure that the function got called
before the write in every place that the file was possibly being written
to and that there weren't multiple process/threads that could write to it.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alain Ketterlin  
View profile  
 More options Nov 9 2012, 1:42 pm
Newsgroups: comp.lang.c
From: Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
Date: Fri, 09 Nov 2012 19:42:05 +0100
Local: Fri, Nov 9 2012 1:42 pm
Subject: Re: Strange Behaviour in finding Size of a File

felix <ckpra...@gmail.com> writes:
> if ( stat ( logFile, &results ) == 0 )

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

-- Alain.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Nov 9 2012, 2:29 pm
Newsgroups: comp.lang.c
From: Keith Thompson <ks...@mib.org>
Date: Fri, 09 Nov 2012 11:29:34 -0800
Local: Fri, Nov 9 2012 2:29 pm
Subject: Re: Strange Behaviour in finding Size of a File

Alain Ketterlin <al...@dpt-info.u-strasbg.fr> writes:
> felix <ckpra...@gmail.com> writes:
>> if ( stat ( logFile, &results ) == 0 )

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

What makes you think that will yield better results, and what is "level 2"?

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <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"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Nov 9 2012, 2:34 pm
Newsgroups: comp.lang.c
From: Keith Thompson <ks...@mib.org>
Date: Fri, 09 Nov 2012 11:34:54 -0800
Local: Fri, Nov 9 2012 2:34 pm
Subject: Re: Strange Behaviour in finding Size of a File

It's been pointed out that 1048576 is the wrong value if you want 10 MB
(more pedantically, 10 MiB).  But LogSize is also the wrong type.
It should be the same type as the st_size member.

You probably want to use "const" rather than "static" in the definition
of LogSize, unless the value can change.

Neither of these is likely to be the cause of the problem you're seeing.
Since stat() is defined by POSIX, not by C, you'll likely get better
answers in comp.unix.programmer.

--
Keith Thompson (The_Other_Keith) ks...@mib.org  <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"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alain Ketterlin  
View profile  
 More options Nov 9 2012, 2:47 pm
Newsgroups: comp.lang.c
From: Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
Date: Fri, 09 Nov 2012 20:47:53 +0100
Local: Fri, Nov 9 2012 2:47 pm
Subject: Re: Strange Behaviour in finding Size of a File

Keith Thompson <ks...@mib.org> writes:
> Alain Ketterlin <al...@dpt-info.u-strasbg.fr> writes:
>> felix <ckpra...@gmail.com> 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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Nov 9 2012, 3:33 pm
Newsgroups: comp.lang.c
From: Keith Thompson <ks...@mib.org>
Date: Fri, 09 Nov 2012 12:33:14 -0800
Local: Fri, Nov 9 2012 3:33 pm
Subject: Re: Strange Behaviour in finding Size of a File

Alain Ketterlin <al...@dpt-info.u-strasbg.fr> writes:
> Keith Thompson <ks...@mib.org> writes:
>> Alain Ketterlin <al...@dpt-info.u-strasbg.fr> writes:
>>> felix <ckpra...@gmail.com> 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) ks...@mib.org  <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"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Alain Ketterlin  
View profile  
 More options Nov 9 2012, 8:08 pm
Newsgroups: comp.lang.c
From: Alain Ketterlin <al...@dpt-info.u-strasbg.fr>
Date: Sat, 10 Nov 2012 02:08:18 +0100
Local: Fri, Nov 9 2012 8:08 pm
Subject: Re: Strange Behaviour in finding Size of a File

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.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Philip Lantz  
View profile  
 More options Nov 9 2012, 9:34 pm
Newsgroups: comp.lang.c
From: Philip Lantz <p...@canterey.us>
Date: Fri, 9 Nov 2012 18:33:57 -0800
Local: Fri, Nov 9 2012 9:33 pm
Subject: Re: Strange Behaviour in finding Size of a File

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.

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Kenny McCormack  
View profile  
 More options Nov 9 2012, 9:58 pm
Newsgroups: comp.lang.c
From: gaze...@shell.xmission.com (Kenny McCormack)
Date: Sat, 10 Nov 2012 02:58:27 +0000 (UTC)
Local: Fri, Nov 9 2012 9:58 pm
Subject: Re: Strange Behaviour in finding Size of a File
In article <MPG.2b075cebd6906329989...@news.eternal-september.org>,
Philip Lantz  <p...@canterey.us> 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...


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Nov 9 2012, 10:14 pm
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Fri, 09 Nov 2012 22:14:56 -0500
Local: Fri, Nov 9 2012 10:14 pm
Subject: Re: Strange Behaviour in finding Size of a File
On 11/09/2012 03:33 PM, Keith Thompson wrote:

> Alain Ketterlin <al...@dpt-info.u-strasbg.fr> writes:
>> Keith Thompson <ks...@mib.org> writes:
>>> Alain Ketterlin <al...@dpt-info.u-strasbg.fr> writes:
>>>> felix <ckpra...@gmail.com> 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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Keith Thompson  
View profile  
 More options Nov 9 2012, 10:24 pm
Newsgroups: comp.lang.c
From: Keith Thompson <ks...@mib.org>
Date: Fri, 09 Nov 2012 19:24:35 -0800
Local: Fri, Nov 9 2012 10:24 pm
Subject: Re: Strange Behaviour in finding Size of a File

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) ks...@mib.org  <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"


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
James Kuyper  
View profile  
 More options Nov 9 2012, 11:30 pm
Newsgroups: comp.lang.c
From: James Kuyper <jameskuy...@verizon.net>
Date: Fri, 09 Nov 2012 23:30:45 -0500
Local: Fri, Nov 9 2012 11:30 pm
Subject: Re: Strange Behaviour in finding Size of a File
On 11/09/2012 10:24 PM, Keith Thompson wrote:

> James Kuyper <jameskuy...@verizon.net> 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

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ian Collins  
View profile  
 More options Nov 9 2012, 11:46 pm
Newsgroups: comp.lang.c
From: Ian Collins <ian-n...@hotmail.com>
Date: Sat, 10 Nov 2012 17:46:16 +1300
Local: Fri, Nov 9 2012 11:46 pm
Subject: Re: Strange Behaviour in finding Size of a File
On 11/10/12 14:08, Alain Ketterlin wrote:

> Keith Thompson<ks...@mib.org>  writes:
>> Alain Ketterlin<al...@dpt-info.u-strasbg.fr>  writes:
>>> Keith Thompson<ks...@mib.org>  writes:

>>>> 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've never seen the expression used in
the context of man pages.

--
Ian Collins


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Philip Lantz  
View profile  
 More options Nov 10 2012, 2:29 am
Newsgroups: comp.lang.c
From: Philip Lantz <p...@canterey.us>
Date: Fri, 9 Nov 2012 23:29:04 -0800
Local: Sat, Nov 10 2012 2:29 am
Subject: Re: Strange Behaviour in finding Size of a File

Kenny McCormack wrote:

> In article <MPG.2b075cebd6906329989...@news.eternal-september.org>,
> Philip Lantz  <p...@canterey.us> 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?

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Nov 10 2012, 8:32 am
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@comcast-dot-net.invalid>
Date: Sat, 10 Nov 2012 08:32:45 -0500
Local: Sat, Nov 10 2012 8:32 am
Subject: Re: Strange Behaviour in finding Size of a File
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
esos...@comcast-dot-net.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Keighley  
View profile  
 More options Nov 10 2012, 9:06 am
Newsgroups: comp.lang.c
From: Nick Keighley <nick_keighley_nos...@hotmail.com>
Date: Sat, 10 Nov 2012 06:06:24 -0800 (PST)
Local: Sat, Nov 10 2012 9:06 am
Subject: Re: Strange Behaviour in finding Size of a File
On Nov 9, 1:24 pm, Eric Sosman <esos...@comcast-dot-net.invalid>
wrote:

> On 11/9/2012 2:09 AM, felix wrote:

> > This method was written to create new Log File, when the size of the Log File reaches a max size defined by user [10MB in our case]. Here is the code snippet that does this check:

> > //-- Code starts here : --

> > static size_t LogSize = 1048576;

>      Ah. This is obviously some strange usage of "10 MB" that
> I hadn't previously been aware of.

> > It is strange that the condition got satisfied when results.st_size = 2589116.

>      Not all *that* strange ...

this is why you don't put numbers like that in your code

#define K 1024
#define M (K * K)
#define LOG_SIZE (10 * M)


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Nick Keighley  
View profile  
 More options Nov 10 2012, 9:09 am
Newsgroups: comp.lang.c
From: Nick Keighley <nick_keighley_nos...@hotmail.com>
Date: Sat, 10 Nov 2012 06:09:06 -0800 (PST)
Local: Sat, Nov 10 2012 9:09 am
Subject: Re: Strange Behaviour in finding Size of a File
On Nov 10, 7:29 am, Philip Lantz <p...@canterey.us> wrote:

no

 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Ben Bacarisse  
View profile  
 More options Nov 10 2012, 10:12 am
Newsgroups: comp.lang.c
From: Ben Bacarisse <ben.use...@bsb.me.uk>
Date: Sat, 10 Nov 2012 15:12:28 +0000
Local: Sat, Nov 10 2012 10:12 am
Subject: Re: Strange Behaviour in finding Size of a File

I'd put an 'L' suffix on the 1024 since 10 * M is outside the minimum
range or plain int arithmetic.

--
Ben.


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Eric Sosman  
View profile  
 More options Nov 10 2012, 10:13 am
Newsgroups: comp.lang.c
From: Eric Sosman <esos...@comcast-dot-net.invalid>
Date: Sat, 10 Nov 2012 10:13:13 -0500
Local: Sat, Nov 10 2012 10:13 am
Subject: Re: Strange Behaviour in finding Size of a File
On 11/10/2012 9:06 AM, Nick Keighley wrote:

     Ugh.  If you simply *must* do that sort of thing, at least
use identifiers like KILO and MEGA (or KIBI and MIBI).  Myself,
I'd prefer to write

        #define LOG_SIZE (10L * 1024 * 1024)

... with special emphasis on the "L".

--
Eric Sosman
esos...@comcast-dot-net.invalid


 
You must Sign in before you can post messages.
To post a message you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Messages 1 - 25 of 36   Newer >
« Back to Discussions « Newer topic     Older topic »