none
Capture TextWindow Output RRS feed

  • Question

  • I need to capture the output of the TextWindow and write it to a file. I have tried File.AppendContents but it is too limited for my needs. Here is some sample code:

    For i = 1 To nop
     TextWindow.Write("x" + i + "_" + i)
     If (i <> nop) Then
      TextWindow.Write("+")
     EndIf
    EndFor
    TextWindow.WriteLine("-0==0;")
    

    Incase your wondering it is generating code for IBM's OPL which optimizes equations using CPLEX. The variable nop is just a number. Anyway there is no File.Write or File.WriteLine (that works like the TextWindow.WriteLine output). Is there anyway I can work around this or just dump the entire text window to a file?

    Thursday, February 17, 2011 8:03 PM

Answers

  • No way to dump the TewxtWIndow output as far as I know, but..

    The approach in SmallBasic I would take is to create the full line (record) output in a string variable or an array or variables with one for each line, then output these to a file, for example:

    nop = 10

    records = "" 'The line output is empty
    'records[1] is the first line of output - if only one line, then an array isn't necessary
    For i = 1 To nop
      records[1] = records[1]+"x" + i + "_" + i
      If (i <> nop) Then
        records[1] = records[1]+"+"
      EndIf
    EndFor
    records[1] = records[1]+"-0==0;"

    fileName = Program.Directory+"\test.txt"
    File.DeleteFile(fileName) 'Possibly delete the file before appending records
    For i = 1 To Array.GetItemCount(records)
      TextWindow.WriteLine(records[i])
      File.AppendContents(fileName,records[i]) 'Just append the record (line)
    EndFor

    • Marked as answer by Spegs21 Thursday, February 17, 2011 9:46 PM
    Thursday, February 17, 2011 8:26 PM
    Moderator

All replies

  • Could you make your own print method:

     

    TRUE = 1
    FALSE = 0
    
    printToFile = TRUE
    
    output = "this is my output"
    print()
    
    Sub print
     
     If printToFile = TRUE Then
     TextWindow.WriteLine("printing to file: " + output) ' (print to file code here ofc)
     EndIf
     
     TextWindow.WriteLine(output)
     
    EndSub
     
    So you assign your print line to that variable output then call print(). You can then control where its printed inside the print() method. Its a pain you can't pass variables to subroutines. 

     

    • Edited by _msanders_ Thursday, February 17, 2011 8:33 PM clearing something up...
    Thursday, February 17, 2011 8:25 PM
  • No way to dump the TewxtWIndow output as far as I know, but..

    The approach in SmallBasic I would take is to create the full line (record) output in a string variable or an array or variables with one for each line, then output these to a file, for example:

    nop = 10

    records = "" 'The line output is empty
    'records[1] is the first line of output - if only one line, then an array isn't necessary
    For i = 1 To nop
      records[1] = records[1]+"x" + i + "_" + i
      If (i <> nop) Then
        records[1] = records[1]+"+"
      EndIf
    EndFor
    records[1] = records[1]+"-0==0;"

    fileName = Program.Directory+"\test.txt"
    File.DeleteFile(fileName) 'Possibly delete the file before appending records
    For i = 1 To Array.GetItemCount(records)
      TextWindow.WriteLine(records[i])
      File.AppendContents(fileName,records[i]) 'Just append the record (line)
    EndFor

    • Marked as answer by Spegs21 Thursday, February 17, 2011 9:46 PM
    Thursday, February 17, 2011 8:26 PM
    Moderator
  • Thanks that worked perfectly.
    Thursday, February 17, 2011 9:46 PM