Scrolling file prblm from MSP
Problem:
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. I am using C programming. Can sombody suggest me a simple and effective way of doing this..??
My Solution:
Initially don’t open the file stream. But allocate (malloc) a 1MB buffer. U have to handle this buffer as a Ring Buffer. ie, If u reach the tail of the buffer, start writing from the head.
Now for this Buffer maintain 4 pointers viz
1. Start Pointer: Always points to the Head of the Buffer
2. Read Pointer: Points to the Start of the valid data in the Buffer.
3. Write Pointer: Points to the address where u can write
4. End Pointer: Always points to the Tail of the Buffer
1 and 4 are always Fixed. 2 and 3 can be moved.
Now Make RP = SP; and WP = SP;
OK?. Now u write the data into the buffer. But before u write, check whether (WP + Size of Instantaneous Data )
LOOP:
Wtite the Data in the address pointed by WP. Update WP = WP + Size of Instantaneous Data.
If not, u have reached the end of the buffer. Now make WP = SP and RP = RP + 20% of Buffer Size. (If RP is also crossing EP, reset it to SP.)Go to LOOP.
If u want to stop writing, I think this is informed to the program by an event/signal. DO the following
Open a file in “W” mode.
if RP > WP
{
Write the Buffer contents from RP to EP
Write the Buffer contents from SP to WP
}else
{
Write the Buffer contents from RP to EP
}
Close the file.Free the malloced space.Exit.
Thats it. HTH. Thnx for the excercise. If u write Byte by Byte this is easy. But if u write in chunks, u need to have one more pointer.
More on the Problem:
The thing is that the whole thing happens dynamically(inside a DLL) and hence I will have to have a file opened straight away. I guess your idea would work when I copy the whole thing into a buffer whenever I exceed 1 MB… Yep that is one way of doing it..
JP’s Solution:
You just divide the 1MB memory in to five files. so that u could exactly remove the 20% of content and u could ready to handle all the files ….i don’t thing how it works for your original needs. It’s just my view….and it need less memory there is no need of 1MB.we could handle it very less memory.Normally in this method used for taking continous log messages and remove it like that. Why i say like this already in one application we handle it like that it work fine.just think …
Further More:
Yah!! thats great to hear good responses already!!! Yeah JP that is also one way of doing it. and in fact the efficient way to do it. My manager is looking for omething like file memory mapping… is it possible!!!!
Thangam’s:
when u open a file have a counter. Then start writing into the file when the file size(counter value) comes to 1MB then reset the file pointer to the starting of the file and reset the counter to 0 and continue writing.
By having the counter value you can get the latest 1MB of data at the end of the program.
Sujai’s
http://msdn.microsoft.com/library/default.asp?url=/library/en-us/dngenlib/html/msdn_manamemo.asp
Finally MSP:
Yep that can be done using fseek operations..What I need is something like a scrolling file….I guess this site sent by sujai gives more info on it…
Still donno whether MSP had completed the implementation, need to chk him.
Add New Comment
Thanks. Your comment is awaiting approval by a moderator.
Do you already have an account? Log in and claim this comment.
Add New Comment