
-----------------------------------
Cervantes
Sat May 21, 2005 8:33 pm

Modding a File
-----------------------------------
Hi all.  I'm having some difficulty getting Turing to modify a text file the way I want it.
I am able to find the line that I want to modify easily enough.  However, when I try to put something there, I get some problems.  (I am using mod, by the way.)  If the string I am inserting is shorter than the string that is already on that line, it doesn't completely erase the data that was on that line.  Similarly, if the string I am inserting is longer than the string that is already on that line, it puts the whole string, but it eats away data from the next line(s)!
Here's an example of what I mean:

var fileNo : int
open : fileNo, "test.txt", mod, put
put: fileNo, "CHANGED"
close : fileNo

And "test.txt" contains this:  (can't use "." in syntax apparently)

Geoff
Stanley

Then the file after running the program will be:

CHANGED
anley


So, my question is, how do I prevent this?  I only want to change one specific line, and I don't want any overlapping.  I assume this is happening because Turing can't insert data at a specific point in the file.  Nor can it change the total size of the file.  I suppose I could get around this by placing lots of spaces after each line, and when I read each line in I use a little String Manipulation to chomp off the extra spaces.  This seems like too much work for something like this, and also forces me to make the file larger.
Thanks in advance,
Cervantes

-----------------------------------
Delos
Sat May 21, 2005 8:49 pm


-----------------------------------
Ever been typing way in Rewrites it (Go AKFG!!!).  Sorry, I couldn't resist that one.

How to fix?  Well, the best way would be to read in most if not all of your text file, edit the required areas using simple string manipulation, then Rewrite the file.  Of course, you will be rewriting the entire file since it would be no use whatsoever doing this if you modded the file and your new file was shorter than the old file...you can fill in what would happen in that case.

-----------------------------------
Cervantes
Sat May 21, 2005 9:30 pm


-----------------------------------
Okay.  I didn't really want to do that because I'm already forced to reading/writing a lot of data at the beginning/end of my program and I didn't want to slow this process down any more.  Oh well, at least it will make th coding easier!

Thanks for the advice, Delos.

-----------------------------------
Tony
Sat May 21, 2005 11:40 pm


-----------------------------------
Oh well, at least it will make th coding easier
I'm sure that Dan will disagree with me on the following (though I'm hoping that wtd will back me up)

But oftentimes you're better off writing code that is easily understood than complex code that benchmarks better.

-----------------------------------
Hikaru79
Sun May 22, 2005 1:31 am


-----------------------------------
It doesn't insert it, it just Rewrites it (Go AKFG!!!).  Sorry, I couldn't resist that one.
Wow, and I thought I was obsessed  :lol: Hats off to you, Delos ;)

-----------------------------------
Cervantes
Sun May 22, 2005 6:53 am


-----------------------------------
Oh well, at least it will make th coding easier
I'm sure that Dan will disagree with me on the following (though I'm hoping that wtd will back me up)

But oftentimes you're better off writing code that is easily understood than complex code that benchmarks better.
This will be easily understood.  It means I'll be working with my array, rather than constantily having to dive into the file and change stuff.  :)

I didn't get any of this rewrite stuff. :?

-----------------------------------
MysticVegeta
Sun May 22, 2005 10:08 am


-----------------------------------
I have to agree with the Rewrite, I have worked with it for my guestbook program and when we try to insert text to a file thats already there, it rewrites the text that was already on the file! What i do for it is that i first "get" the contents in the file and store them in a flexible array and close the stream. Then I write the "other text" + "flexible array text" on the file. Also, if i want to change the flexible array text, i manipulate it in the loop.

-----------------------------------
StarGateSG-1
Tue May 24, 2005 11:12 am


-----------------------------------
There is another way around rewriting the entire file. You can have each section of the text assigned to a variable in an array and then when you replace it, you do it through vasr and not rewrite over the file! Note, you must be able to use read and write, put and get will only cause the problem,but say at my school read and write crash the pogram because our board likes having lots of restrictions. Look in the help file in will aid you with why that happens, it actually tells that will happen.

-----------------------------------
MysticVegeta
Tue May 24, 2005 3:23 pm


-----------------------------------
 have each section of the text assigned

I dont follow this part.
