Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Modding a File
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Cervantes




PostPosted: Sat May 21, 2005 8:33 pm   Post subject: 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:
Turing:

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)
test_txt:

Geoff
Stanley

Then the file after running the program will be:
test_txt:

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
Sponsor
Sponsor
Sponsor
sponsor
Delos




PostPosted: Sat May 21, 2005 8:49 pm   Post subject: (No subject)

Ever been typing way in [insert favourite word processor here] when by mistake you hit the Insert key? And then you go back and try to add something in, but for some reason it overwrites stuff from before? Isn't that annoying?

Well, that's what Turing does. It grabs a position in the file (returned by your tell and seek tandem) and puts stuff there. It doesn't insert it, it just 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




PostPosted: Sat May 21, 2005 9:30 pm   Post subject: (No subject)

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




PostPosted: Sat May 21, 2005 11:40 pm   Post subject: (No subject)

Cervantes wrote:
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




PostPosted: Sun May 22, 2005 1:31 am   Post subject: (No subject)

Delos wrote:
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 Laughing Hats off to you, Delos Wink
Cervantes




PostPosted: Sun May 22, 2005 6:53 am   Post subject: (No subject)

Tony wrote:
Cervantes wrote:
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. Smile

I didn't get any of this rewrite stuff. Confused
MysticVegeta




PostPosted: Sun May 22, 2005 10:08 am   Post subject: (No subject)

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




PostPosted: Tue May 24, 2005 11:12 am   Post subject: (No subject)

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.
Sponsor
Sponsor
Sponsor
sponsor
MysticVegeta




PostPosted: Tue May 24, 2005 3:23 pm   Post subject: (No subject)

StarGateSG-1 wrote:
have each section of the text assigned


I dont follow this part.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 9 Posts ]
Jump to:   


Style:  
Search: