Reading a Text File.......

TheMightySpud

Boy Genius - Sort of.
Hi all,

Hope someone can help me out with this is as it's driving me nuts.

I'm trying to read a simple 2 line text file and then use that data to load an object into Layout.

The loading of the Object file is all fine and dandy, but I'm having trouble finding out how to read a Textfile.

I found something similar on the forums from a while ago, but struggling to get it to read the contents of the file. I'm sure I'm doing something very simple wrong.

Current Code is below.

Code:
@warnings
@version 2.2
@name read_file

generic
{
debug();


f = File("C:\\Users\\Public\\CKVFX\\output1.txt","r") || error("no file selected!");
x=1;
while(!f.eof())
	{

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

	line_001[x] = linevar[1];
	line_001[x] = linevar[2];
	x++;
	}

LoadObject("line_001[1]");

f.close();
}

The debug panel shows that the 'output1.txt' file is found etc. but the array etc. stays empty.

Anyone have any ideas ? :-/

Thanks
TheMightySpud
 

TheMightySpud

Boy Genius - Sort of.
Okay, so I've now edited the code after realising a silly mistake.

All the variables now get populated (confirmed by the debug panel). However, I'm still getting a 'Can't find Object File' error. So very confusing.

Code:
@warnings
@version 2.2
@name read_file

generic
{
debug();


f = File("C:\\Users\\Public\\CKVFX\\output1.txt","r") || error("no file selected!");
while(!f.eof())
	{
	buf = f.read();
	if( !buf ) break; // or check whether string length is > 0
	linevar = parse(",",buf);

	line_001 = linevar[1];
	line_002 = linevar[2];
	}

LoadObject("line_002");

f.close();
}

TheMightySpud
 

TheMightySpud

Boy Genius - Sort of.
Okay, so hopefully just one last question. Is it possible in lScript to 'force' the script to read whitespace?

I'd rather not have to go through a bunch of renaming to remove said spaces from file/folder names :-/

TheMightySpud
 

TheMightySpud

Boy Genius - Sort of.
Oh dammit. lol. Just as I saw this reply I noticed that my generated text file didn't have the extra carriage return added to it. All fixed now, silly thing. Thanks for the reply(ies) though Denis, really appreciate it. :)
 

TheMightySpud

Boy Genius - Sort of.
Okay, so lscript syntax is driving me insane........any ideas why the following 'case' block isn't working properly? It just 'jumps' to the Default case every time, even though the data for 'line_001' is present and matches the case labels.

The data is being pulled from a text file in the format : <assetType>,<fullAssetPath>,<assetFolderPath>

I can get it working using a crapload of if/else statements, but figured a switch()/case method would be a little more elegant.....

Code:
@warnings
@version 2.3
@name read_file

generic
{

AutoConfirm(1);

current_dir = getdir("Content");

f = File("C:\\Users\\Public\\CKVFX\\tempText.txt","r") || error("no file selected!");

while(!f.eof())
{
	buf = f.read();
	if( !buf ) break; // or check whether string length is > 0
	linevar = parse(",",buf);
	
	line_001 = linevar[1];
	line_002 = linevar[2];
	line_003 = linevar[3];
}

ContentDirectory(line_003);


switch(line_001)
{
case staticObject:
	info(line_002);
	LoadObject(line_002);
	break;

case genericTexture:
	info(line_002);
	myimage=loadimage(line_002);
	break;

default:
	//info("No Matches found!");
	info(line_001);
	break;
}
	
f.close();

ContentDirectory(current_dir);
AutoConfirm(0);

}

Thanks
TheMightySpud
 

TheMightySpud

Boy Genius - Sort of.
Well, integers didn't work, I suspeect because the data being read from the text file is being handled as a string? (replaced the first line of the text file with 1, changed the code to case 1,2 etc.) and still just jumps to the 'default' state.
 

TheMightySpud

Boy Genius - Sort of.
Gah. Now I need to figure out how to take the string data from the text file and turn it into an integer. *bangs head on desk*
 
Top Bottom