Amiga.org

Operating System Specific Discussions => Amiga OS => Amiga OS -- Development => Topic started by: Jose on April 12, 2005, 07:47:06 PM

Title: Adding to a growing file in the filesystem with C...
Post by: Jose on April 12, 2005, 07:47:06 PM
What's up dudes8-) . Been long since I read filesystem stuff so I'd like some advice.
In the program I'm doing there's an option to make it add info to a log if some kinds of actions happen. No prob, I thought, just make an Amiga linked list and add a new node to it when new info is needed. But, theoretically at least, if I leave it up running for a week, the list might use all spare RAM. So I just need a way to save a bunch of nodes to the HD from time to time, when a certain limit is reached. The problem is, I want to keep it all on the same file.
And by the way, even if this is possbile the file will be fragmented if another file is written between two updates right ?
Title: Re: Adding to a growing file in the filesystem with C...
Post by: CrazyProg on April 12, 2005, 09:40:06 PM
The fragmentation of the file is unavoidable (as far as I know). :-(  
You could minimise it by creating bigger blocks for the file, keep writing data to the file untill the block is full then creating another big (empty) block for the file and filling it etc. :-)
Another thing that you could do is do a sort of log rotate thing.  When creating the file create it with a date imbeded in the name (YYYY-MM-DD is best that way 'dir' will put them in order) write you data to the file close it and don't use it again. :-)

Title: Re: Adding to a growing file in the filesystem with C...
Post by: Cymric on April 12, 2005, 09:50:56 PM
I don't really see the problem---why don't you leave the file open for writing? The FS won't close it on its own, after all: it will wait patiently for you to close it.

The fragmentation is nearly unavoidable. Perhaps you can counter it to some extent by reserving space in the form of a really big file, sufficiently sized to contain the complete log, and then gradually overwrite it, making sure you pad the stuff you write out to the exact same size as the original dummy file. But that is a wild guess at best, relying on inner workings of the FS you should really not be relying on. Why is a non-fragmented file so important anyway? The FS will handle the fragmentation transparently when you read the file back into memory.

Methinks you are trying to solve a problem which really is not a problem :-).
Title: Re: Adding to a growing file in the filesystem with C...
Post by: Jose on April 14, 2005, 07:26:48 PM
The fragmentation is not really a problem, but I prefer to not leave the file open. I guess I just need a way to append data to the file and the DOS library already provides functions to do that but I don't remember... (Me thinks I got to read the filesystem DOS functions again...)
Title: Re: Adding to a growing file in the filesystem with C...
Post by: Cymric on April 14, 2005, 07:57:30 PM
Seems like you want an Open() with MODE_OLDFILE, and then a Seek() to the end, and hey presto, you can begin Write()ing ;-).
Title: Re: Adding to a growing file in the filesystem with C...
Post by: Thomas on April 14, 2005, 09:15:26 PM

You must not write to a file opened with MODE_OLDFILE. There is MODE_READWRITE for this.

if (f = Open("logfile",MODE_READWRITE))
{
Seek (f,0,OFFSET_END);
FPrintf (f,"%s\n",logmessage);
Close (f);
}

Bye,
Thomas
Title: Re: Adding to a growing file in the filesystem with C...
Post by: Piru on April 14, 2005, 09:19:00 PM
@Thomas

Nonsense. Cymric is right.

From dos/Open autodoc:
Quote

"The named file is opened and a file handle returned.  If the accessMode is MODE_OLDFILE, an existing file is opened for reading or writing. If the value is MODE_NEWFILE, a new file is created for writing. MODE_READWRITE opens a file with an shared lock, but creates it if it didn't exist."


(bold mine)


However, in this particular case MODE_READWRITE is better, as it will create the file if it doesn't exist. So in the end you're partially right too (but writing is perfectly ok to MODE_OLDFILE, too!).
Title: Re: Adding to a growing file in the filesystem with C...
Post by: Jose on April 15, 2005, 02:07:24 PM
Cool! I'll try those, seems really easy...