Writing to middle of a file

Discussion to talk about software related topics only.
Post Reply
MastaThf
Posts: 6
Joined: Fri Apr 03, 2009 11:32 am

Writing to middle of a file

Post 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
User avatar
yevgenit
Posts: 84
Joined: Fri Apr 25, 2008 12:47 am
Contact:

Re: Writing to middle of a file

Post 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.
Yevgeni Tunik
Embedded/RealTime software engineer
https://www.linkedin.com/in/yevgenitunik/
________________________
Ridgeglider
Posts: 513
Joined: Sat Apr 26, 2008 7:14 am

Re: Writing to middle of a file

Post 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.
rnixon
Posts: 833
Joined: Thu Apr 24, 2008 3:59 pm

Re: Writing to middle of a file

Post 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?
Post Reply