Solved

Circular file

Posted on 2006-07-24
3
311 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
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…
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.
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

628 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