PDA

View Full Version : How create a external File?


thorsten hartmann
01-06-2011, 05:48 PM
Hi Guys,

I want create a File, for example "data.txt". And i want this text as data.

# Name
# Level
[grid]
1;Ux;Uy;Uz;45
2;Ux;Uy;Uz;45
3;Ux;Uy;Uz;45
4;Ux;Uy;Uz;45
[end]


Thanks for help.

mfg
hot chip

TheGrak
01-06-2011, 07:34 PM
I just became interested in this as well, and after a quick search a few days ago, I came across this bit of code. I can't remember where I saved it from, so if you know who wrote it, chime it. ( :

This script exports the currently selected models height, width, and length to a .csv file.


-------------------------------------------
-- ExportAsCVS.ms
-- exports the currently selected models
-- height, width, length (in that order)
-------------------------------------------
global theFileStream
fn exportAs fileName = (
-- create file
theFileStream = createFile fileName;

if theFileStream != undefined then (
-- file ok to write
selection = getCurrentSelection()

if selection.count > 0 then (
-- write header
printToFile ("x, y, z, Pieces, Name")
-- write data
for obj in selection do (
_name = obj.name
_height = floorToNDecimals(obj.height) as string
_width = floorToNDecimals(obj.width) as string
_length = floorToNDecimals(obj.length) as string
-- assuming pieces is always 1
_pieces = "1"
-- write to file
printToFile (_length+","+_width+","+_height+","+_pieces+","+_name)
)
) else (
-- error
printToFile ("Error: Nothing Selected!")
)
)
printToFile ("Yeah!")

-- close
close theFileStream;
)

fn printToFile theString t:0 = (
-- auto tab (empty string)
tabString = " ";
tabString = substring tabString 1 (t*4);
-- print
format "%%\n" tabString theString to:theFileStream;
)

fn floorToNDecimals theNumber N:2 = (
return floor( ((pow 10 N) * theNumber) ) / (pow 10 N)
)

exportAs("Test2.csv");


Loading in the values can be done thru the 'stringStream' thingy in maxscript. Check the help.

thorsten hartmann
01-06-2011, 08:56 PM
Hi Garrik,


thank you very much, i will try that.

mfg
hot chip

PEN
01-06-2011, 10:25 PM
f=createFile ((getDir #scripts)+"myFileName.txt")
format "data: %" "some thing to put in the file" to:f
close f

lo
01-07-2011, 06:35 AM
If you have all your text in array before writing I find that

(dotnetClass "System.IO.File").WriteAllLines <filename> <string array>

works much faster

thorsten hartmann
01-07-2011, 09:00 AM
Oh guys,


thank you very much. I will try the codes.

mfg
hot chip

PEN
01-07-2011, 12:27 PM
If you have all your text in array before writing I find that

(dotnetClass "System.IO.File").WriteAllLines <filename> <string array>

works much faster

So this is faster then format? I'll have to give it a try. Thanks.

Gravey
01-07-2011, 01:10 PM
If you have all your text in array before writing I find that (dotnetClass "System.IO.File").WriteAllLines <filename> <string array> works much faster So this is faster then format? I'll have to give it a try. Thanks.if you're interested in speed, my guess would be that formatting everything to a stringstream and then formatting the resulting stringstream to a file would be faster (and safer than regular formatting to a file too). My reasoning is that the array of mxs strings wouldnt need to be converted to a dotnet string array before the WriteAllLines function can execute which i dont think scales as well as the stringstream method for large outputs. Please note that my thoughts have not been benchmarked...

denisT
01-07-2011, 11:25 PM
So this is faster then format? I'll have to give it a try. Thanks.

my guess is that format has to be faster if you put only one string (it doesn't matter how long the string is).

however if the task doesn't need any particular file format i always save(export) data to XML. it might be slower at the write/read stage but much faster at parse/search.

LoneRobot
01-11-2011, 10:29 PM
I would second that, XML would be perfect for the sort of thing you are trying to store. My thinking - Is performance really an issue for strings of this length? I wouldn't think so.

TheGrak
01-12-2011, 06:49 PM
(I don't mean to hijack this thread, and I think this relates closely with creating an external file)
I'm writing data to an external file, then trying to load that data back in. This is the code so far:

f=createFile "test.objekt"
format "line1:%" "data1\n" to:f
format "line2:%" "data2" to:f
close f
o=openFile "test.objekt" mode:"r+"
myData = readDelimitedString o "line2:"
print myData

Gives error: Unable to convert: <File:C:.../test.objekt> to type: String
I'm sure I'm missing something obvious, a syntax error. Or perhaps I'm not loading the data in? Hmm

lo
01-12-2011, 07:54 PM
I'm not sure you can write to a file without supplying a full path. Is the file actually being created, and if so, where?

TheGrak
01-12-2011, 07:57 PM
lo,
the file is being created at:<File:C:\Documents and Settings\Administrator\Desktop\grakScripts\test.objekt>
the script is creating the test.objekt file in the same directory as the script thats running. I think thats the default location if you don't supply a dir, I'm probably wrong on that one though.

TheGrak
01-12-2011, 08:06 PM
Got it working

f=createFile "test.objekt"
format "line1:%" "data1\n" to:f
format "line2:%" "data2" to:f
close f
o=openFile "test.objekt" mode:"r+"
skipToString o "line2:"
myReadData = readChars o 5
print myReadData

Needed to read in the data! :)

Here's a better way of doing that (can read in the whole line instead of certain number of characters)

f=createFile "test2.objekt"
format "Name:%" "myName\n" to:f
format "Type:%" "myType\n" to:f
format "Size:%" "155.548\n" to:f
format "Created:%" "01.12.11\n" to:f
close f
o=openFile "test2.objekt" mode:"r+"
skipToString o "Size:"
myReadData = readLine o
print myReadData
close o

thatoneguy
01-12-2011, 09:36 PM
I do it slightly differently in that I create a stringstream and then at the end of the string-stream do a:

format stringstreamVar to:fileVar

what I like about doing it that way is you can put all of your file creation at the end of the string creation and a crash or error won't leave an orphaned file.

Also instead of using "createfile" I'll often do:

openFile filePath mode:"w"

which will create a file if it doesn't exist, or overwrite an existing file.

TheGrak
01-12-2011, 09:59 PM
thatoneguy - awesome tips man! i'll be adopting your methods :)

CGTalk Moderation
01-12-2011, 09:59 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.