Notifications
Clear all

[Closed] Writing out to a text file – Problem

Hi everyone,

I am writing out information to a new script file (effectively a text file). I am having no problem filtering through the textfile using the “readDelimitedString”,“readline” and “readvalue” commands, which are all very useful. But then once I have found the place I need to be in the textfile, I then need to write text to the file.

The problem is that if I write a lot of text to the file using the

format “Text that needs inserting” to:Textfile

command then it writes the text at the required place, but writes it over what ever text subsequently follows in the file!

Is there a way to insert text, so that it pushes forward the subsequent text that all ready exists in the file, rather than simply overwriting it?!

If there is a solution I would be eternally grateful, since it would put the finishing touches to a little project that I am putting together.

Thanks everyone.

Everzen

5 Replies

You may be approaching your problem the wrong way. using the functions:

skipToString <binstream> “stringtofind”
seek <binstream> 0 – to reset the pointer to the beginning of the file for the next skipToString function
and readValue

should be very helpful, how is your file structured? How are you writing to it and why can’t the text go on a separate line, is there any rhyme or reason?

-Colin

Messed around with things a little, and ive got the searching for the point to start writing a bit smoother (thanks ). I am fine with writing anything to a new line, but when I do write something then it is still writing over text that appears later in the document. Say as a random example that I have a text file that says:

–File to Store Nice Object information

Object Name:

Object Position:

Where all I want to do is add the Object name after “Object Name:” and then its position after “Object Position” Now I can search through the text and find the “Name:” string, but if I try and write the name into the file after that string using

format “Ball2” to:Textfile (for example)

Then when I open the text file I get the following text:

–File to Store Nice Object information

Object Name:

Ball2Position:

As you can see, instead of inserting the text on the new line, and pushing the text in the rest of the document forward, it just writes straight over any text that follows in the document, which is really messing things up! (In this case it has written over the word “Object”)

I am sure I am doing something really stupid, I have written out plenty of stuff to text files before, but I have never had to insert text into the middle of a text file, and it is really doing my head in

Any pointers would be really handy!

Thanks again MoonDoggie,

Everzen

mytest.txt


 #height 20
 #hsegs 3
 #radius 50
 

 f = openFile "c:\\mytest.txt"
 skipToString f "#height"
 h = readValue f
 seek f 0 
 skipToString f "#hsegs"
 hs = readValue f
 seek f 0 
 skipToString f "#radius"
 r = readValue f
 
 b = box height:h hsegs:hs
 s = sphere radius:r
 

It’s simple, and that’s what Maxscript essentials says you should try

AFAIK, there is no “insert” when writing to files.
Your best bet is:

*Open the source file
*Create a temp file
*Read each line from the source file
*Output each line to the temp file
*When you reach the line that needs to be inserted, write it to the temp file
*Keep on copying the rest of the source to the temp until the end.
*Close both files.
*Delete source file.
*Rename temp. file to be named like the source file.

It is relatively fast…

Cheers for the example code MoonDoggie, things are definitely operating a lot smoother, I was being a little long winded about it!

Cheers Bobo, as always a clear and concise solution. I was worried that was probably going to end up being the solution, but didnt want to go that way in case I was missing something simple. As it is, Ill just work my way through those steps which shouldn’t be too much of a problem at all!

Thanks guys, your time is really appreciated.

All the best,

EverZen