Solved

Log File Implementation.

Posted on 2004-08-04
6
236 Views
Last Modified: 2012-05-05
Hi,
      I have to create a file and write into it.

Now in my program I start writing into the file.Now when I go more than 1 MB I will have to delete the first 20% of the file and shift the remaining to the top and start appending to the File...this should continue for ever until my program runs.

can sombody suggest me a simple and effective way of doing this..
0
Comment
Question by:deardeepan
6 Comments
 
LVL 4

Expert Comment

by:alikoank
ID: 11712994
> I will have to delete the first 20% of the file and shift the remaining to the top and start >appending to the File

The easiest way will be:

. when the file reaches treshold size open a new file
. copy the last %80 of original file into new file
. delete original file
. rename new file to original file.
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11713127
look into log file rotation ... It will keep log file size in check and offer you far more flexibility
http://iain.cx/src/logrotate/

The approach you are using is not suitable for most applications .... Logrotate or some similar utility will fare better
What alikoank suggested is the way to go if you want to remove a part of the file
0
 

Author Comment

by:deardeepan
ID: 11713280
I am using Windows platform..

Is there any other way to do this..I will not be able to do log rotate wither.

is there somethinglike memap in UNIX ....equivalent in windows
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 45

Expert Comment

by:sunnycoder
ID: 11713296
ok since your file size is small, you can do this

read entire file in 1Mb buffer

memmove last three quarters to the beginning of the buffer ...

write the buffer back to the file
0
 
LVL 22

Accepted Solution

by:
grg99 earned 125 total points
ID: 11714582
>ok since your file size is small, you can do this

>read entire file in 1Mb buffer

>memmove last three quarters to the beginning of the buffer ...

>write the buffer back to the file

Er, how about just skip the memmove and:
    int twenty = sizeof(buffer) / 5;
    write( theFile,  buffer[  twenty  ], twenty * 4 );
0
 
LVL 45

Expert Comment

by:sunnycoder
ID: 11715112
good idea grg99
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Why does module make constant click noise ? 9 141
Problem in finding output of a program 11 108
sameEnds challenge 3 187
Crashing when deleting value with no child in Binary Tree 4 25
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use nested-loops in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

713 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question