Notifications
Clear all

[Closed] How to Read/SAve File contents


if not persistents.isPersistent "you_name" do
( 
  persistents.make "you_name"
  globalVars.set "you_name" #()
)

it’s how it should be…

OK, now i have another problem with the binary write/read code:

I execute this code:


createFile ((getDir #renderPresets)+"\TempRP.rps")

filepre = (getDir #renderPresets + "\TempRP.rps")

ss = fopen filepre "wb"

print ss

fflush ss

FClose ss

deleteFile (getDir #renderPresets + "\TempRP.rps")


and in the last line, when i try to delete the file it says false and the file is not deleted, i closed the stream.

I’m with this from two hours now…i can’t understand, some ideas?

Cheers.

P.S.: thanks for your answer denisT, now i know two ways to do that, also i did not know the “execute” command, so is great to sometimes

Ok, at last i found it, a garbage collection GC() solved it

Cheers.

you don’t have to use createFile in your case… createFile creates text format <FileStream>.

in your code you create FileStream and don’t close it. It’s why you can’t delete the file after that.

the code should be:


rp = getDir #renderPresets + "\\TempRP.rps" -- always use double backslash
ss = fopen rp "wb" -- returns BinStream if success
--print ss -- doesn't make any sense -- ss is BinStream
fflush ss
fclose ss
deleteFile rp

EDIT1: READING YOUR POST denisT, i was writing at the same time than you

Ok, the problem is still there.

There is huge and strange…

If i call the command:

deleteFile (getDir #renderPresets + “\TempRP.rps”)

from my script or from the listener after writing data to it (and flushed and closed all) it returns false and cannot be deleted.
The file is created by me, so maybe there is a problem, because it seems that there is no happening any data write if i try to write it from the script because the size of the file remains 0Kb.
Also if i do a GC() from the script nothing changes, but if i do a CG() from the listener and use the above command, then suddenly it returns true and the file is deleted.

Really strange!

nothing is strange… “\TempRP.rps” is wrong! “\T” means tabulation…

close MAX, reopen it, and try my code…

Now i understand, i was creating to streams totally different, so i was closing one, but not the other, now i do as you say and the delete problem is gone.

But now there is one last problem, and once is solved all the binary stream story is closed (i hope, YOHOOOO!)

And this problem i’m sure that will explain me why not to use “execute” command.

When i try to write to the file with the line:


execute ("for b in "+ RPsaveName+" do WriteByte zz b")

where RPsaveName is the variable where is stored my global persistent variable name.
In this case nothing is written, but if i do the same thing but specifying the name manually:


for b in BS0BASE do WriteByte zz b

The file is written as it must be.

I’m thinking about how to do this (because i cannot know the name of the persistent variable, is created by the user), is there a workaround?

Cheers and thanks for all the help you give to me.

Why would you want the user to define the name of a variable? That just sound like a really bad idea…

1 Reply
(@cerberusc)
Joined: 11 months ago

Posts: 0

The user is not completly free to define the name of the variable, also i touch the name and add some letters to ensure the variable is unique and is within my script only.
It’s like when you are saving the render preset, you can define the name of the file, well here is the same but with a variable that will contain the preset, something like this.

Ok, i think i got it, execute does not acces to loop variables, i’ll try a work around, and post what i get.

Thanks

Well that’s kind of my point, wouldn’t it be better AND more useful to just choose one unique name for the variable and use always the same? Is there any reason why you NEED the variable to change names?

It just sounds weird that you are using a variable to store the name of a variable…

Page 4 / 5