Solved

Circular file

Posted on 2006-07-24
3
292 Views
Last Modified: 2013-11-20
Hi everybody,
I need to write a file that i want to limit the size to 800 Kb and when have this size, start to write from the top of the file and
deleting this first line and so on.
How can i do it.

Thanks.
0
Comment
Question by:SalvaThor
  • 2
3 Comments
 
LVL 1

Expert Comment

by:reidy_boy
ID: 17177076
Store your data in an array first.  Declare an array of that size in your program.  Write to the array and starting at the beginning when you get to the end.  When you are ready to write the file, just reference the array.
0
 
LVL 2

Accepted Solution

by:
snyp earned 125 total points
ID: 17186047
why not just keep track of how much you've written to the file and then fseek() your file when you hit your limit?

however.. let's say you're at position 0xC7FFF and you want to write 0x1000 bytes.. try the following..
you'll need to know the size of the data you're writing obviously..

#define SIZELIMIT 0xC8000
char dataArray[512];
unsigned long dataSize;
unsigned long writeSize;
unsigned long filePos;

..

filePos = ftell(filePtr);

if (filePos + dataSize >= SIZELIMIT) {

writeSize = dataSize - ((filePos + dataSize) % SIZELIMIT); // write as much as we can up to the end of the 800kb
fwrite(&dataArray[0], writeSize, 1, filePtr);

fteek(filePtr, 0, 0); // back to the start of the file
fwrite(&dataArray[((filePos + dataSize) % SIZELIMIT)], dataSize - writeSize, filePtr); // write the remainder at 0

}

else
fwrite(&dataArray[0], dataSize, 1, filePtr); // data is small enough to fit, just write it all
0
 
LVL 2

Expert Comment

by:snyp
ID: 17186065
i'm sorry, i need to correct the code there..

fseek(filePtr, 0, 0); // back to the start of the file
fwrite(&dataArray[writeSize], dataSize - writeSize, filePtr); // write the remainder at 0
0

Featured Post

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Suggested Solutions

Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Along with being a a promotional video for my three-day Annielytics Dashboard Seminor, this Micro Tutorial is an intro to Google Analytics API data.

773 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