Page 1 of 1
Writing to middle of a file
Posted: Wed Apr 22, 2009 6:52 am
by MastaThf
Hello All,
I have a log (text) file with multiple lines of strings in it.
I want to move my pointer around inside the file and then
write some text.
I was going to use F_SEEK to move my pointer where I want it
but then writing will overwrite previous data.
Is there some way to go back into the middle of this file
and add text without overwriting the text that was there?
Or should I just leave blank spaces that I can overwrite later?
Thanks
Re: Writing to middle of a file
Posted: Wed Apr 22, 2009 7:43 am
by yevgenit
Write the changed content of the original file to the temporary file. Then, rename the original file to the backup name, and rename the temporary file to the original name.
Possibly, the simplest way is the best.
Re: Writing to middle of a file
Posted: Wed Apr 22, 2009 8:43 am
by Ridgeglider
FYI:
If your text file is on SD, and if you have a lot of files in the directory, I've found that the functions to find, open and close the file may take quite a while, particularly with long filenames. I think this may be because f_open works on a text search of the file name (maybe using f_findfirst, and subsequently f_findnext)? Once you have the fp (ie after f_open) things seem to go more quickly, particularly if you write in big chunks and not char by char. One work around I've used is to keep a buffer file open so you can write fast when needed w/o having to wait for the open. Once again though, closing or flushing seems to take a bit longer, but you can often do that in the background. Know that files that have not been closed or flushed lose their contents if things go south...
If others have found a similar bottleneck and/or workaround, I'm interested too.
Re: Writing to middle of a file
Posted: Wed Apr 22, 2009 3:49 pm
by rnixon
Hi,
It might help to know what you are trying to do. Appending is a standard type of file function. Insetting in the middle is not. I think you would have the same issues with a windows or linux file system. What is the high level end goal that inserting data in the file will accomplish?