Notifications
Clear all

[Closed] Getting info to and from textfiles

Hi guys,

Having fun with writing to textfiles.
With some help I got the reading in bit sorted.
I’ve reread and reread the help file but can’t work out how you write back to a file.
Attached are the ideas and functions.

The reason for the array is so that I can easily change scopes later for rollouts etc.
The variables will be all sorts of different sorts – strings, integers and floats.
The main reason for this is to implement a load and save defaults function in a rollout.
v_count is just so I can easily add and delete variables without changing the loops.

thanks

17 Replies

Alex,

A fast an easy way to store settings with your script are the setINISetting and getINISetting commands.

If you still want to use your method:

createFile returns the handler to the created file (just like openFile, so you don’t need to call openFile afterwards).

So, in your case you could say:

– you need the fileStream value to read/write a file
– <fileHandler> is just a variable-name I made up
fileHandler = createFile savname

createFile returns undefined if the file
– could not be created
if fileHandler != undefined then
(

format “Some String…
to:fileHandler
– etc.
– etc.

– close the file when all writing is done…
close fileHandler

) else (
messagebox “Unable to create file!”
)

Hope this helps

Martijn

Thanks Martijn,

OK here’s my take on this for the saving of an array to a tab delimited text file – still not working

Had a look at the INI method but need the flexibility of an open array for now…until the whole floater is completed and I know the total variable structure.

Here’s the error message:

def_save()
– Error occurred in def_save()
– Frame:
– v_array: #(1, 2, 3, 4)
– No “map” function for 4
OK

any ideas

Alex,

Please post the script again so I can see what the def_save() function does.

Martijn

oops here it is:

There’s a little typo in this line:
for i in v_array.count do

should be
for i = 1 to v_array.count do

or you could change the loop as follows:
for i in v_array do
(
format “% ” (i as string) to:fh
)

I’ve attached a quickly adapted script…

Thanks Martijn,

That seems to work. I’ve now got an array of values in v_array.

As part of the load defaults section, whats the best way to get these back into a floater as say spinner values across two rollouts?

At this point all the values in v_array are strings. I’m interested in how you would structure the script in terms of variable scope, and making sure that v_array is able to be updated and then saved out again.

I’ve tried this several ways including using structures for the variables and am getting very confused, so its principles that are needed at this stage.

To convert your string <v_array> to an actual array, you can use:
v_array = execute v_array

note: the execute statement can slow down your script when used repeatedly

Another way to read the value from the file and directly convert it to a string:

(i suppose you now use readLine to read your file)
try: readExpr
This reads AND evaluates the next MAXScript expression from the file…

So if your file looks like:
#(1,2,6,20)
#(false, false, true, false)

and you read the file using readExpr, it will return the actual arrays, NOT strings


fileHandler = openFile “test.txt”
while not (eof fileHandler) do
(
– read the string and evaluate it, the result
– is stored in <v_array>
v_array = readExpr fl
)

for the readExpr statement to work properly, your file has to be formatted like a regular script (statements seperated by a ; or a CRLF)…

good luck!

Thanks again Martijn,

I’ll check out the readExpr stuff. In the meantime here is a script which shows a bit better what I am trying to do.

I think there are problems with the way I have structured it in terms of scope as well as the problem of getting the variables to and from the array(s)

One thing about global versus local variables; globals are stored through an entire max-session, while locals only exist in the scope in which they were created.

in the line:
global defname = getOpenFileName …

you don’t need to use a global variable here, as it is only used in the scope it is created in:
local defname = getOpenFileName …

about your script;

  • put the structure definitions outside of the script scope (put it above the first ‘(’.

  • use locals instead of globals where possible.

  • v_array is an empty array and doesn’t get filled anywhere in the script, so the save function will always output an empty file… You’ll probably want to fill the array with the vvmvars variables before saving the file…

  • Martijn

Page 1 / 2