Page 1 of 2 12 LastLast
Results 1 to 15 of 16

Thread: Reading a Text File.......

  1. #1

    Reading a Text File.......

    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

  2. #2
    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

  3. #3
    Member
    Join Date
    May 2006
    Location
    France
    Posts
    4,082
    I think that line_002 is a string variable,
    so no need "" in LoadObject().

    Denis.

  4. #4
    Told you it was something simple. lol.

    Works great, thanks Denis

    TheMightySpud

  5. #5
    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

  6. #6
    Member
    Join Date
    May 2006
    Location
    France
    Posts
    4,082
    Quote Originally Posted by TheMightySpud View Post
    Okay, so hopefully just one last question. Is it possible in lScript to 'force' the script to read whitespace?..
    Do you get issues with white spaces in folder or filename?
    Should not be a problem, in Lscript or Python.

    Denis.

  7. #7
    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.

  8. #8
    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

  9. #9
    Member
    Join Date
    May 2006
    Location
    France
    Posts
    4,082
    I never used text string in switch()
    did you try "staticObject" as case.

    Denis.

  10. #10
    Certainly did, does the same thing. Going to try using integers instead and see if that works.

    TheMightySpud

  11. #11
    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.

  12. #12
    Member
    Join Date
    May 2006
    Location
    France
    Posts
    4,082
    Absolutly, except binary reading, all is string,
    you need integer()

    Denis.

  13. #13
    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*

  14. #14
    LightWave documentation BeeVee's Avatar
    Join Date
    Feb 2003
    Location
    Pessac
    Posts
    5,107
    As Denis says, no? Use integer(string)?

    B
    Ben Vost - NewTek LightWave 3D development
    LightWave 3D Trial Edition
    AMD Threadripper 1950X, Windows 10 Pro 64-bit, 32GB RAM, nVidia GeForce GTX 1050Ti (4GB and 768 CUDA cores) and GTX 1080 (8GB and 2560 CUDA cores) driver version 430.86
    AMD FX8350 4.2 GHz, Windows 7 SP1 Home Premium 64-bit, 16GB RAM, nVidia GeForce GTX 1050Ti (416.34, 4GB and 768 CUDA cores)
    Dell Server, Windows 10 Pro, Intel Xeon E3-1220 @3.10 GHz, 8 GB RAM, Quadro K620
    Laptop with Intel i7, nVidia Quadro 2000Mw/ 2GB (377.83 and 192 CUDA cores), Windows 10 Professional 64-bit, 8GB RAM
    Mac Mini 2.26 GHz Core 2 Duo, 4 GB RAM, 10.10.3

  15. #15
    Ah sorry, completely misunderstood what Denis posted. My bad.

Page 1 of 2 12 LastLast

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •