PDA

View Full Version : File Stream problem.


Guiniture
06-05-2008, 11:29 AM
Hi,

I have a simple problem that I know using file stream should solve but im confused when using file stream.
Here is what i want to do:

I have a grid of boxes in a scene, some are red and some are white. What i want to do is store their positions and colors on file so that if i can upload them easily later.

So firstly i have made an array and stored the information in binary form:


boxpos = #()
for i in allboxes do
(
if (i.material.diffuse as string) == (color 252 0 0 a string) do (append boxpos 1)
if (i.material.diffuse as string) == (color 255 255 255 as string) do (append boxpos 0)
)


So know i have an array which holds all the information I need, I want to save this array to a text file on my desktop. Then I also need to know how to open the text file and get the array back into max.

If anybody can help that would be great!

Cheers.

decapitator
06-05-2008, 11:58 AM
Here is an example of saving/loading an array writing the array is a bit wierd added the main array in another array.
Dont know if its me or maxscript why just print array to:filestream prints each item on a seperate line now, in another script of mine its fine with a bitarray might be different in saving.
(
--saving part
if (doesFileExist "c:/test.txt") == false then --check for file
fs = createFile "c:/test.txt" --create if its not there
else
fs = openFile "c:/test.txt" mode:"w" --else open it mode w for writing

--can be shortened to:
--if (fs = openFile file mode:"w") == undefined do fs = createFile file

somearray = #("item1","item1"); --set random array
print #(somearray) to:fs --print the array to the filestream resource
close fs --close the fs

--loading part
fs = openFile "c:/test.txt" mode:"r" --open file for reading
loadedArray = execute fs --reads the filestream and executes it
close fs --close the filestream

print loadedArray --prints the loaded array
)
Together with the manual this should be enough for you to translate it to your needs.

Guiniture
06-05-2008, 12:26 PM
Thanks for that! I managed to write the array to file and reload it into a new maxscript window.

But the problem i have now is that the reloaded array only contains the first 20 digits. :


#(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, ...)

I have 2400 digits in the array altogether. I have tried reading the file using : readDelimitedString, but that did not seem to work.

Is this a problem with the way I am writing the array to file?

Thanks again for the help.

decapitator
06-05-2008, 12:35 PM
Take a look at http://forums.cgsociety.org/showpost.php?p=5179216&postcount=2 on the part how I read the string, you might have to execute it seperatly then but that seemed to work on larger files acording to him.

Guiniture
06-05-2008, 11:45 PM
Thanks for the help, this seemed to do the trick in the end.

for download:

fs = createfile c:\test.txt

for i in array_01 do
(
print #(i) to: fs
)
close fs

for upload:

fs = openfile "c:/test.txt"
while eof fs == false do
(
w = readline fs
append array_01 (w as integer)
)
close fs

Bobo
06-06-2008, 02:54 PM
Thanks for that! I managed to write the array to file and reload it into a new maxscript window.

But the problem i have now is that the reloaded array only contains the first 20 digits. :


#(0, 0, 0, 1, 0, 0, 0, 0, 0, 0, 1, 1, 0, 1, 0, 0, 0, 0, 0, 0, ...)

I have 2400 digits in the array altogether. I have tried reading the file using : readDelimitedString, but that did not seem to work.

Is this a problem with the way I am writing the array to file?

Thanks again for the help.

Check out the topic "PrintAllElements Variable and Context" in the MAXScript Help!

CGTalk Moderation
06-06-2008, 02:54 PM
This thread has been automatically closed as it remained inactive for 12 months. If you wish to continue the discussion, please create a new thread in the appropriate forum.