I'm trying to control the size of a log file. When it gets more than 5k
oversize, I attempt to trim it thusly:
$fcap = intval(($dval * 0.0108))*1024; // what it should be
$fsize = filesize($file); // what it is
$oversize = $fsize - $fcap;
if ( $oversize > 5120 ) // 5k oversize
{
$fp = fopen($file,"r+");
fseek($fp, -$fcap, SEEK_END);
$redux = fread($fp, $fsize);
$fp = fopen($file,"w");
fwrite($fp, $redux);
}
The problem is the file is getting wiped out - reduced to 0k
Doesn't the fseek statement say "move the pointer to the end of the file,
then back up by the amount of $fcap" ? Then, data from the pointer down is
read into $redux, and $redux overwrites $file ?
Am I missing something? Why is my log file getting wiped out? 3 2414
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
deko wrote: I'm trying to control the size of a log file. When it gets more than 5k oversize, I attempt to trim it thusly:
$fcap = intval(($dval * 0.0108))*1024; // what it should be $fsize = filesize($file); // what it is $oversize = $fsize - $fcap;
if ( $oversize > 5120 ) // 5k oversize { $fp = fopen($file,"r+"); fseek($fp, -$fcap, SEEK_END); $redux = fread($fp, $fsize); $fp = fopen($file,"w"); fwrite($fp, $redux); }
The problem is the file is getting wiped out - reduced to 0k
Doesn't the fseek statement say "move the pointer to the end of the file, then back up by the amount of $fcap" ? Then, data from the pointer down is read into $redux, and $redux overwrites $file ?
Am I missing something? Why is my log file getting wiped out?
Hi,
First, the first fopen() should probably use mode "r".
Second, you're not closing the file after the first use, so this might
screw things up. Put an fclose($fp); after the fread() line.
Third, shouldn't the fread() line be reading $fcap bytes, not $fsize
bytes? Reading $fsize bytes means trying to go beyond EOF (which
according to the documentation isn't a problem), while reading $fcap
bytes means you get *exactly* what you want.
If nothing works, try some error checking on every single line inside
the loop: they *all* return values.
Chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFBP8hmgxSrXuMbw1YRAlW9AKDVhi8Fk8jDEqEuO7i6nS HdtKcWfQCeJEdN
pMAKFD6k8E9J3ChZizeAxxw=
=yT4W
-----END PGP SIGNATURE-----
> First, the first fopen() should probably use mode "r".
I think I remember trying this but it did not work for some reason. I'll
try it again. Second, you're not closing the file after the first use, so this might screw things up. Put an fclose($fp); after the fread() line.
That part of the code was omitted.
Third, shouldn't the fread() line be reading $fcap bytes, not $fsize bytes? Reading $fsize bytes means trying to go beyond EOF (which according to the documentation isn't a problem), while reading $fcap bytes means you get *exactly* what you want.
perhaps...
If nothing works, try some error checking on every single line inside the loop: they *all* return values.
Actually, I got it working - the issue was a file lock routine that was not
part of my post.
Thanks for the reply.
On Thu, 09 Sep 2004 04:09:57 +0000, deko wrote: Actually, I got it working - the issue was a file lock routine that was not part of my post.
I think that it is a good idea to call 'clearstatcache' before 'filesize'.
DG This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: Leslaw Bieniasz |
last post by:
Hello,
I am trying to fastly read large binary files (order of 100-200 MB)
using ftell() and fseek(). My class gets a pointer to the
data stored in the file, and then uses fseek() to access
and...
|
by: Christopher Benson-Manica |
last post by:
On thinking about the "replace a word in a file" thread, I wondered how easy
it would be to accomplish the same thing with only one file pointer. This led
me to some questions...
"For a text...
|
by: TJ Walls |
last post by:
Hello All,
I am baffled ... I am trying to improve the speed of a program
that I have written that performs random access within a file. It
relies heavily on fseek and is very slow. To test, I...
|
by: Xenos |
last post by:
Can anyone tell me what the standard says about using fseek (on a binary
file) to seek past the end-of-file? I can't find anything in my (draft)
copy of the standard, nor in the FAQ.
Thanks
|
by: John |
last post by:
I have a file open using fopen and am reaing using fread.
Does this mean that the OS (linux) already buffers this
file? What is the buffer size? Can I change it?
Thanks,
--j
|
by: cedarson |
last post by:
I am writing a program and have been instructeed to use the 'fseek',
'ftell', and 'stat' functions, however, after looking in the online
manual for each of these, I am still unsure on how to use...
|
by: ericunfuk |
last post by:
I have posted a few other posts before this one today, I still can't
solve my problem, I think I need to elaborate my problem now.
I'm trying to send a file using UDP, below is a segment of my...
|
by: ericunfuk |
last post by:
If fseek() always clears EOF, is there a way for me to fread() from an
offset of a file and still be able to detect EOF?i.e. withouting using
fseek(). I also need to seek to an offset in the file...
|
by: thomas.mertes |
last post by:
Hello
Recently I discovered some problem. I have some C code
which determines how many bytes are available in a
file. Later I use this information to malloc a buffer of
the correct size before...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: nemocccc |
last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: Hystou |
last post by:
Overview:
Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
|
by: tracyyun |
last post by:
Dear forum friends,
With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |