Solved

Circular file

Posted on 2006-07-24
3
272 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
Comment Utility
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
Comment Utility
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
Comment Utility
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

Do You Know the 4 Main Threat Actor Types?

Do you know the main threat actor types? Most attackers fall into one of four categories, each with their own favored tactics, techniques, and procedures.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Separate into files by filename 12 66
List out all word 7 219
modThree challenge 4 64
ShiftLeft challenge 21 65
Introduction: Displaying information on the statusbar.   Continuing from the third article about sudoku.   Open the project in visual studio. Status bar – let’s display the timestamp there.  We need to get the timestamp from the document s…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

772 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now