Solved

Circular file

Posted on 2006-07-24
3
301 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

Independent Software Vendors: 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
repeatEnd java challenge 42 85
Creating Labels and Frequency list style reports in SAS code 9 110
Looking for a specific application/software 2 119
sameEnds challenge 3 187
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
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.

679 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