Welcome, Guest. Please login or register.

Author Topic: Adding to a growing file in the filesystem with C...  (Read 1117 times)

Description:

0 Members and 1 Guest are viewing this topic.

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Adding to a growing file in the filesystem with C...
« 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 ?
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline CrazyProg

  • Newbie
  • *
  • Join Date: Oct 2004
  • Posts: 14
    • Show only replies by CrazyProg
Re: Adding to a growing file in the filesystem with C...
« Reply #1 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. :-)

 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Adding to a growing file in the filesystem with C...
« Reply #2 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 :-).
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Adding to a growing file in the filesystem with C...
« Reply #3 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...)
\\"We made Amiga, they {bleep}ed it up\\"
 

Offline Cymric

  • Hero Member
  • *****
  • Join Date: Nov 2002
  • Posts: 1031
    • Show only replies by Cymric
Re: Adding to a growing file in the filesystem with C...
« Reply #4 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 ;-).
Some people say that cats are sneaky, evil and cruel. True, and they have many other fine qualities as well.
 

Offline Thomas

Re: Adding to a growing file in the filesystem with C...
« Reply #5 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

Offline Piru

  • \' union select name,pwd--
  • Hero Member
  • *****
  • Join Date: Aug 2002
  • Posts: 6946
    • Show only replies by Piru
    • http://www.iki.fi/sintonen/
Re: Adding to a growing file in the filesystem with C...
« Reply #6 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!).
 

Offline JoseTopic starter

  • Hero Member
  • *****
  • Join Date: Feb 2002
  • Posts: 2869
    • Show only replies by Jose
Re: Adding to a growing file in the filesystem with C...
« Reply #7 on: April 15, 2005, 02:07:24 PM »
Cool! I'll try those, seems really easy...
\\"We made Amiga, they {bleep}ed it up\\"