473,394 Members | 1,761 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Constant file size

Hi, there!
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?

Thank you in advance.
--
Adrián E. Córdoba

Aug 17 '06 #1
7 3042
If you need logging, I suggest looking at the log4net project, found at
the Apache Foundations site. They have a LogFileAppender, which has
the functionality you describe.

HTH
Andy
Adrián E. Córdoba wrote:
Hi, there!
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?

Thank you in advance.
--
Adrián E. Córdoba
Aug 17 '06 #2
Sorry, Andy. But I think LogFileAppender always add new lines to the
log file, so the file increase its size.
I need to keep constant the size of the log file.

Thank you, for your comments.
--
Adrián

Andy ha escrito:
If you need logging, I suggest looking at the log4net project, found at
the Apache Foundations site. They have a LogFileAppender, which has
the functionality you describe.

HTH
Andy
Adrián E. Córdoba wrote:
Hi, there!
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?

Thank you in advance.
--
Adrián E. Córdoba
Aug 17 '06 #3
Adrián E. Córdoba <so***********@gmail.comwrote:
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?
Two things:

1) Keeping the same number of lines *won't* keep the file the same
size, unless all lines are the same size

2) Deleting the first line in a file involves rewriting the whole file.
If you use a fixed-size encoding (eg Encoding.Unicode) and keep the
lines the same size, you can make it a rolling log file, writing into
the middle of the file, overwriting lines on a rolling basis.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 17 '06 #4
Thank you for your comments, Jon.

1) It is not necessary the file size will be exactly XX MB. It's enough
to keep constant the number of lines.

2) Working with a "rolling log file" and writting in the middle of the
file, make the file hard to read if you open the file with simple text
applications like Notepad because you need to find the oldest line in
the file. It is possible but it isn't desirable.

Thank you, again.

--
Adrián
Jon ha escrito:
Adrián E. Córdoba <so***********@gmail.comwrote:
I'm developing a little application which must record some events in a
log file (.txt).
How can I delete the first line in the log file every time I add a new
line at the end, in order to keep constant the file size?

Two things:

1) Keeping the same number of lines *won't* keep the file the same
size, unless all lines are the same size

2) Deleting the first line in a file involves rewriting the whole file.
If you use a fixed-size encoding (eg Encoding.Unicode) and keep the
lines the same size, you can make it a rolling log file, writing into
the middle of the file, overwriting lines on a rolling basis.

--
Jon Skeet - <sk***@pobox.com>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Aug 18 '06 #5
Adrián E. Córdoba wrote:
Thank you for your comments, Jon.

1) It is not necessary the file size will be exactly XX MB. It's enough
to keep constant the number of lines.
Okay.
2) Working with a "rolling log file" and writting in the middle of the
file, make the file hard to read if you open the file with simple text
applications like Notepad because you need to find the oldest line in
the file. It is possible but it isn't desirable.
In that case, unless you *really* want to rewrite the whole log file
each time, I suggest you roll over to different log files (deleting old
ones) rather than try to keep it all in one log.

Jon

Aug 18 '06 #6
Just to add to what Jon said, if you do go the rolling log method,
log4net does support that natively. No need to write your own.

Andy

Jon Skeet [C# MVP] wrote:
Adrián E. Córdoba wrote:
Thank you for your comments, Jon.

1) It is not necessary the file size will be exactly XX MB. It's enough
to keep constant the number of lines.

Okay.
2) Working with a "rolling log file" and writting in the middle of the
file, make the file hard to read if you open the file with simple text
applications like Notepad because you need to find the oldest line in
the file. It is possible but it isn't desirable.

In that case, unless you *really* want to rewrite the whole log file
each time, I suggest you roll over to different log files (deleting old
ones) rather than try to keep it all in one log.

Jon
Aug 18 '06 #7
Thanks to all.

--
Adrián

Andy ha escrito:
Just to add to what Jon said, if you do go the rolling log method,
log4net does support that natively. No need to write your own.

Andy

Jon Skeet [C# MVP] wrote:
Adrián E. Córdoba wrote:
Thank you for your comments, Jon.
>
1) It is not necessary the file size will be exactly XX MB. It's enough
to keep constant the number of lines.
Okay.
2) Working with a "rolling log file" and writting in the middle of the
file, make the file hard to read if you open the file with simple text
applications like Notepad because you need to find the oldest line in
the file. It is possible but it isn't desirable.
In that case, unless you *really* want to rewrite the whole log file
each time, I suggest you roll over to different log files (deleting old
ones) rather than try to keep it all in one log.

Jon
Aug 18 '06 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Jani Yusef | last post by:
I have a HW problem stated as shown at the top of the solution. The thing is is that I am not 100% sure wtf constant memory means. Well, I think I do but I am confused. Does my solution use contant...
15
by: Lasse Skyum | last post by:
I have my own string class that I'm generally verry fond of compared to std::string. For that same reason I'm trying to improve a little on it. Since manny objects in my project will contain...
7
by: news.hku.hk | last post by:
suppose i have a class abc, and i have an important value stored as int integer = 888; when i want to declare an array of objects: abc obj; // error said non-constant....expect constant...
48
by: Nirvana | last post by:
How to make the font size constant in HTML code, so that in a web browser it remains fixed. For e.g in IE if you press CTRL and move mouse wheel front or back the font size changes, cheers
8
by: Tony Johansson | last post by:
Hello Experts! What does this mean actually. If you have a template with a type and non-type template argument, say, like this template<typename T, int a> class Array {. . .}; then A<int,...
14
by: Urs Thuermann | last post by:
What is the most elegant way to check certain conditions at compile time? I.e. I want a compile time error to be generated if for example the size of a struct is not a multiple of 4 or if one...
6
by: niklaus | last post by:
Hi, I have seen that the following code compiles in some environments like devc++ but fails on some env's like gcc on linux. Can someone tell if "int *au=malloc(sizeof(int)*10);" is a constant...
4
by: Hans | last post by:
Hi, I want to define a couple of constant strings, like in C: #define mystring "This is my string" or using a const char construction. Is this really not possible in Python? Hans
2
by: dpicella | last post by:
I have a semi-urgent need to solve a problem in Java. I have three small images that I need to hold to a constant size (e.g., 1 inch) regardless of both monitor size and resolution. Basically...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
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...
0
Oralloy
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,...
0
jinu1996
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...
0
tracyyun
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 using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.