PDA

View Full Version : Read file problem



lertola2
09-14-2010, 08:38 PM
Hi all,

I am taking my first stab at writing an lscript. Ultimately I want the script to read number data from a text file, clone an object and position and scale the clone according to values in the text file. Unfortunately I am getting stuck on the first bit, reading the file. The script below does read a file but when it gets to the end of file it continues in the 'while(' loop causing an error.

@warnings
@version 2.2
@name read_file

generic
{
debug();
filename = getfile("Load settings","*.txt",0,1) || error("no file selected!");

f = File(filename,"r") || error("no file selected!");
x=1;
while(!f.eof())
{
linevar = parse(",",f.read());
scale_val[x] = linevar[1];
x_val[x] = linevar[2];
y_val[x] = linevar[3];
x++;
}
f.close();
}

This is the text in the file that I am trying to test the script with:

1.4, -1, 0
1.5, 0, 0
1.6, 1, 0
1.7, 2, 0

Instead of exiting the loop after reading the four lines it tries to read a fifth line. I get an error message that says 'Line 14, invalid argument 2 type to parse()'. Any help or advice would be appreciated.

Thanks,
-Joe

TripD
09-14-2010, 11:48 PM
I'm a total noob, but maybe try putting a blank line at the end of the text file.

Sensei
09-16-2010, 04:08 PM
Try changing this

linevar = parse(",",f.read());

to

buf = f.read();
if( !buf ) break; // or check whether string length is > 0
linevar = parse(",",buf);

lertola2
09-16-2010, 09:31 PM
Thanks Sensei, that worked. Does the Break command exit out of the curly brace block it is in? It is not listed in the lscript command reference.

Thanks,
-Joe


Try changing this

linevar = parse(",",f.read());

to

buf = f.read();
if( !buf ) break; // or check whether string length is > 0
linevar = parse(",",buf);

Sensei
09-16-2010, 11:00 PM
break; and continue; commands are regular c/c++ language commands. Read c/c++ manual for more info.

They work on currently open loop (for,while,repeat-until). If there is more loops nested, exit the top most, they're used, regardless of other brackets made by f.e. if().

lertola2
09-17-2010, 04:11 AM
What is the relationship between c and lscript? Can you use all c commands in lscript?

break; and continue; commands are regular c/c++ language commands. Read c/c++ manual for more info.

Sensei
09-17-2010, 06:53 AM
To simplify life, in the most of current languages, controlling flow of program commands are the same. So, programmer doesn't have to learn new things each time, but naturally use them like in c/c++. Honestly I have never wrote any Lscript.. ;)
But I wrote 200+ real LightWave plugins in c/c++..