Game Development Community

FileObject & Append & Truncate

by amaranthia · in Torque Game Builder · 05/05/2008 (9:25 pm) · 3 replies

Is there a way to append text in the middle of a file, not at the end? Is there an easy way to delete several lines in the middle of a file?

#1
05/06/2008 (9:21 am)
Not natively.

Your best option here (other than integrating a simple database) would be to read in each line into a data structure (keeping track of order), doing your inserts/deletes/other in memory, then writing the file back out again when complete.
#2
05/06/2008 (9:52 am)
I'm no expert, but I'm betting there is no "easy" way to do this. However, I have an idea how you could possibly do it in a not-so-easy way.

You probably need to read in the file to be modified, line by line, while writing it back out to a new file, line by line. Then when you get to the line you want removed or the location of the line you want to add, do the appropriate thing there.

If you can store the InFile's data in memory somehow, you could read the whole file in, make the changes, and read it out.

But another option is, read/write one line at a time into a temporary file, then delete the original and rename the new file.


I'm thinking of how you could do this in script only using the FileObject. If you want to look at the fileObject source code its possible you could find another way.
#3
05/06/2008 (10:57 am)
I've asked Bryce to build me a function in the engine. As soon as he's finished it, I'll share it will all. :)