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

 
 
felix
Guest
Posts: n/a
 
      11-09-2012
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
 
Reply With Quote
 
 
 
 
James Kuyper
Guest
Posts: n/a
 
      11-09-2012
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
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      11-09-2012
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
http://www.velocityreviews.com/forums/(E-Mail Removed)d
 
Reply With Quote
 
John Gordon
Guest
Posts: n/a
 
      11-09-2012
In <(E-Mail Removed)> felix <(E-Mail Removed)> 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
(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Mark Bluemel
Guest
Posts: n/a
 
      11-09-2012
On 09/11/2012 15:16, John Gordon wrote:
> In <(E-Mail Removed)> felix <(E-Mail Removed)> 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.

 
Reply With Quote
 
James Kuyper
Guest
Posts: n/a
 
      11-09-2012
On 11/09/2012 10:16 AM, John Gordon wrote:
> In <(E-Mail Removed)> felix <(E-Mail Removed)> 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.
 
Reply With Quote
 
Greg Martin
Guest
Posts: n/a
 
      11-09-2012
On 12-11-08 11:09 PM, 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 )
> {
> 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
>


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.
 
Reply With Quote
 
Alain Ketterlin
Guest
Posts: n/a
 
      11-09-2012
felix <(E-Mail Removed)> writes:

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


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

-- Alain.
 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-09-2012
Alain Ketterlin <(E-Mail Removed)-strasbg.fr> writes:
> felix <(E-Mail Removed)> 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) (E-Mail Removed) <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
 
Keith Thompson
Guest
Posts: n/a
 
      11-09-2012
felix <(E-Mail Removed)> writes:
> 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.


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) (E-Mail Removed) <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
 
 
 
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