1. #1
    Senior Member Follow User Gallery
    Join Date
    Jul 2014

    Question Write to file

    I'm trying to write a script to loop over the subtools and export them as a text file.
    So far so good , except it will truncate at 255 bytes with about 30 subtools and long names. I've increased the memsize to no avail. I also tried to write each line at at time, to no vail.

    I've also taken a look at Marcus' write to file script and able to save a text file that's over that amount. I'm not sure how much you can allocate as text and then dump to a file.

    // Loop over all subtools,
    // Grab subtool name, write to file

    [VarDef, myWriteStr, ""]
    [VarDef, memOffset, 0]
    [VarDef, myBytes, 0]
    [VarDef, newLine, [StrMerge, [StrFromAsc, 13], [StrFromAsc, 10]]] //
    [VarDef, myFilePath, ""]
    [VarDef, myFileName, "subtools.txt"]

    [RoutineDef, WriteMem,
    [MemResize, ZB_TextOutputMem, [MemGetSize, ZB_TextOutputMem] + 1024]
    [VarSet, myBytes, [MemWriteString, ZB_TextOutputMem, pStr, memOffset, 0]]
    [VarSet, memOffset, memOffset + myBytes]
    [MemResize, ZB_TextOutputMem, memOffset]
    , pStr]//end routine

    [IButton, "Write subtools names", "Writes out all sub tools names!",

    // create variable equal to number of sub tools
    [VarDef, len, [SubToolGetCount]]

    // DEFine the string
    [VarDef, msg, ""] // Define it (once when script is loaded)
    [VarSet, msg, ""] // set it at restart
    [VarDef, tempStr, ""]
    [VarSet, tempStr, ""]

    // loop over array

    // select current sub tool

    // change subtoolName to the current subtool name
    [VarSet, subtoolName, [IGetTitle,Tool:Subtool:Item Info]]

    // shorten the string so it doesn't have a . at the end
    [VarSet, subtoolName, [StrExtract, subtoolName, 0, [StrLength, subtoolName]-2]]

    // set the message to concatenate
    [VarSet, msg, [StrMerge, msg, subtoolName, newLine]]
    [VarSet, tempStr, [StrMerge, tempStr, subtoolName, newLine]]
    [VarInc, i] // i++

    [MemDelete, ZB_TextOutputMem] // just makes sure this isn't around
    [MemCreate, ZB_TextOutputMem, 1024] // create memblock
    [VarSet, memOffset, 0]
    [VarSet, myBytes, 0]

    // write to file
    [VarSet, myWriteStr, tempStr]
    [RoutineCall, WriteMem, myWriteStr]

    ] // end loop

    // write to file
    //when done, write memblock to file...
    [VarSet, myFilePath, myFileName] // path to file - this example will be in the same location as this zscript
    [VarSet, lBytes, [MemSaveToFile, ZB_TextOutputMem, myFilePath, 1]] // will overwrite existing file

    [MemDelete, ZB_TextOutputMem] //all done, delete memblock
    [If, lBytes > 0,
    [Note, [StrMerge, [Val, lBytes], " bytes written to file:\n", myFileName],, 2]

    , //else an error
    [Note, [StrMerge, "Error in writing file, dude! ", [Val, lBytes]],, 5]

    ] // end if

    // show number of subtools
    [Note, [StrMerge, "Number of subtools: ", i, "\n", msg],,2]

    ] // end button

  2. #2
    Moderator Follow User Gallery
    Join Date
    Jun 2004


    The truncating is happening because you create the memory block inside the loop, so it gets recreated with each iteration. Also, you don't want to concatenate each string with with the one before otherwise you'll get a lot of repeats in the file.

    The other problem is that a string variable can only hold a maximum of 255 characters so you need to find another way to display the output in a Note (as your msg variable will be truncated). Fortunately you can use a Note with a time of -1 which then displays with the next Note shown which has a time other than -1. I've used the one which displays the "bytes written to file" message.



Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts