Notifications
Clear all

[Closed] beginner level question – open a file for writing

After hours of trying to find a solution, as soon as I post a message asking for help I get the answer.

12 Replies

The getOpenFileName() method simply returns a file name the user selected.
Once you have the name, you have to specify the mode as in

theFile = openFile theFileName mode:“a+”
format “Something: %” theValue to:theFile
close theFile

or “r+” or whatever mode you want (see the Reference for “FileStream Values”).
“a+” appends the writes to the existing file.

Man you’re quick. Thanks.

I deleted my post because it was a bit messy – but yeah, the answer was putting in “mode:“a+” – I forgot the “mode:” bit – schoolboy error.

Maybe because I am sitting beneath an air conditioning vent my brain freezes from time to time

I hate that my main problem is schoolboy errors rather than “proper” challenges

Ooh well, since you’re around – can you let me know how to seacrh the file for a string, then jump before that string?

would I be right in saying its

seek “what I’m looking for” thefilestream

But then how do I jump before that?

PS – saw the video of you at Siggraph donning the leather jacket – very cool

Hello MyNewCat,

Which Video ? I would like to see it. Do you know where i can find it ?

Thank in advances,
ting

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Hmm, somewhere on the main page are the CGtalk videos that cover Siggraph… it’s in one of them, cant remember which – it happens for a short while, Bobo dons a leather jacket.

Serious guys, I be needing to bump this one up as I’m struggling

 
---------------------------------------------
 -- opening and writing data into the HTM file
 ---------------------------------------------
 
 
 fhtm = openFile asset_file mode:"r+"
 skipToString fhtm ("justin")
 
 print  "
temp string cleaned up for CG talk!" to: fhtm
 
 close fhtm

Ideally I’d like to be able to insert that next before whatever comes next without overwriting it.
However, at the vbery least I need to be able to jump back and remove the “justin” string as after searching for it, I appear after it. And that means the terrorists have already won.

Anyone?

1 Reply
(@bobo)
Joined: 11 months ago

Posts: 0

Since text files do not provide random access writing, you cannot directly add stuff INSIDE the text file without overwriting things.
The trick I use without letting the terrorists get away with it:

Open one file for reading.
Open one file for writing (with a temp name)
Go through the first file, read line by line and output each line into the second file.
Once you find the offending line, instead of outputting it, replace it with whatever you want and output that.
The rest of the lines until the EOF will be output too. (So your loop should be WHILE NOT EOF theFileStream DO…)
Once you are done, close both files, rename the input file to a backup name, rename the output file to the original name of the input file.
Done.

OK – so I can read in the file, read up to the line I want… and in theory it works… but when I actually format the string I want into the file, it always comes up a couple of lines below the string I searched for, which incidentally is returned as the current line.

Help anyone?


– opening and writing data into the HTM file


fhtm = openFile asset_file mode:“r+”

current_line = readline fhtm

print current_line

while current_line != “justin” do

(

skiptonextline fhtm

current_line = readline fhtm

print current_line

)

print “currentline”

print current_line

format “Never let a punk get away with murder – gunshots gunshots all you hearda” to: fhtm

print “currentline after tax”

print current_line

close fhtm

Sorry for late reply bobo – was moved onto another project, but I just implemented what you suggested and it works a treat – I will therefore not have to revoke your Maya master status!

this is what I came up with if anyone searches for this thread and needs some more tips.

Ps – I shall be posting the completed tool on scripspot once it’s done and make notice here – so if anyone wants a piece of it they can.

Thanks again Bobo.

---------------------------------------------
 -- opening and writing data into the HTM file
 ---------------------------------------------
 
 
 fhtm = openFile asset_file mode:"r"
 
 thehtml2file = asset_file + ".temp"
 
 fhtm2 = createFile thehtml2file 

 while not eof fhtm do
	(
  current_line = readline fhtm
	  
   if current_line == "justin" then
	  (
   format  "temp text
" to: fhtm2
   format  "temp text
" to: fhtm2
   format  "temp text
" to: fhtm2
   exit
   ) else (
   format "%
" current_line to: fhtm2
   )
   
  
 )-- end while not eof fhtm
 
 close fhtm
 close fhtm2 
 
 deleteFile asset_file
 renameFile thehtml2file asset_file

HEY!!! 😮
I haven’t turned to the Dark Side!
I am a MAX Master!
A MaXster, if you want.
See my profile…

Page 1 / 2