PDA

View Full Version : read/edit text file



jeremyhardin
08-26-2003, 01:58 PM
If anybody has any ideas, here's my situation. I want to edit a scene file like a text file.
specifically:

go to a blank line
write in my data


seems simple enough, but i cant seem to get lscript to move through the file.
i have a simple search and replace script going, but it doesn't "move" through the file. It searches the whole thing and replaces all instances. also, i'm having trouble writing multiple lines into the middle of a file.

scenes = getdir("Scenes");
chdir(scenes);
savescene("temp.lws");
if((input = File("temp.lws","r"))==nil)
return;
if((output = File("temp2.lws","w"))==nil)
return;

while(!input.eof())
{
line = input.read();
line ~= r~^to be replaced~replacement text~;
output.writeln(line);
}

input.close();
output.close();


any ideas or input are appreciated

Tom Speed
08-26-2003, 11:38 PM
Hi,

I run your script on a text file, it worked fine and changed:

ROB
TOM
JOE
TOM
TEST


into this:

ROB
THOMAS
JOE
THOMAS
TEST

Using expression: line ~= r~^TOM~THOMAS~;

So it's definately working, are you using the latest version of lscript 2.6.1?


Writing multiple lines is easy enough, just depends how you know when to write them. If you were looking to add lines after every replacement you could just check and see if anything was replaced:

while(!input.eof())
{
line = input.read();
linecopy = line;
line ~= r~^TOM~THOMAS~;
if(line != linecopy)
{
// line was changed.... write extra info
output.writeln(line);
// extra lines here
}
else
output.writeln(line);
}

Or something similar.

Cya
Tom