Is There way to write to a text file direct without Stream
I am using a %GlobalStream to create as % stream.Object and writing that to a file but for some reason when I run that on the terminal I get <PARAMETER>zWrite^%Library.GlobalCharacterStream.1 and no file is created
I have tried the following here is my code and still get the same result any help appreciated
ClassMethod WriteNodes(myfile As %String) { set status=##class(%XML.TextReader).ParseFile(myfile,.textreader) set ptemp=##class(%GlobalCharacterStream).%New() //check status if $$$ISERR(status) {do $System.Status.DisplayError(status) quit} //iterate through document, node by node while textreader.Read() { Do ptemp.Write("Node ", textreader.seq, " is a(n) ") Do ptemp.Write( textreader.NodeType," ") If textreader.Name'="" { Do ptemp.Write( "named: ", textreader.Name) } Else { Do ptemp.Write( "and has no name") } Do ptemp.Write( " path: ",textreader.Path) If textreader.Value'="" { Do ptemp.Write( " value: ", textreader.Value) } } set adapter = ##class(%File).%New("C:\IT\50_TestIn\warddata1.txt") set status = adapter.Write(ptemp) /* set tStream=##class(%Stream.FileCharacter).%New() set pInput=##class(%Stream.Object).%New() set pInput=ptemp set ptemp="" set myfile="C:\IT\50_TestIn\warddata1.txt" Set tStatus =tStream.LinkToFile("Myfile") do tStream.Write(pInput) */ }
Comments
set ptemp=##class(%GlobalCharacterStream).%New()set ptemp=##class(%Stream.FileCharacter).%New() set ptemp.Filename="C:\IT\50_TestIn\warddata1.txt"set adapter = ##class(%File).%New("C:\IT\50_TestIn\warddata1.txt") set status = adapter.Write(ptemp)set status = ptemp.%Save()
[@ vitaliy.Serdtsev] have change my code as per your suggestion still not writing to a file I get <PARAMETER>zWrite^%Stream.FileBinary.1
Thanks missed the Do ptemp.WriteLine()
The method Write expects one parameter but you pass a few, and therefore an error occurs.
The following corrected code works for me:
<FONT COLOR="#000080">ClassMethod </FONT><FONT COLOR="#000000">WriteNodes(</FONT><FONT COLOR="#ff00ff">myfile </FONT><FONT COLOR="#000080">As %String</FONT><FONT COLOR="#000000">)
{
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">status</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%XML.TextReader</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">ParseFile</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">myfile</FONT><FONT COLOR="#000000">,.</FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">if $$$ISERR</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">status</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#800080">{</FONT><FONT COLOR="#0000ff">do $System</FONT><FONT COLOR="#008080">.Status</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">DisplayError</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">status</FONT><FONT COLOR="#000000">) </FONT><FONT COLOR="#0000ff">quit</FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#000080">##class</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008080">%Stream.FileCharacter</FONT><FONT COLOR="#000000">).</FONT><FONT COLOR="#0000ff">%New</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#0000ff">set </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Filename</FONT><FONT COLOR="#000000">=</FONT><FONT COLOR="#008000">"C:\IT\50_TestIn\warddata1.txt"
//check status
//iterate through document, node by node
</FONT><FONT COLOR="#0000ff">while </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Read</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#008000">"Node " </FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">seq </FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#008000">" is a(n) "</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">(</FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">NodeType </FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#008000">" "</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">If </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Name</FONT><FONT COLOR="#000000">'=</FONT><FONT COLOR="#008000">""
</FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">( </FONT><FONT COLOR="#008000">"named: "</FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Name</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#0000ff">Else
</FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">( </FONT><FONT COLOR="#008000">"and has no name"</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">( </FONT><FONT COLOR="#008000">" path: " </FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Path</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#0000ff">If </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Value</FONT><FONT COLOR="#000000">'=</FONT><FONT COLOR="#008000">""
</FONT><FONT COLOR="#800080">{
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Write</FONT><FONT COLOR="#000000">( </FONT><FONT COLOR="#008000">" value: " </FONT><FONT COLOR="#000000"> </FONT><FONT COLOR="#800000">textreader</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">Value</FONT><FONT COLOR="#000000">)
</FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">WriteLine</FONT><FONT COLOR="#000000">()
</FONT><FONT COLOR="#800080">}
</FONT><FONT COLOR="#0000ff">Do </FONT><FONT COLOR="#800000">ptemp</FONT><FONT COLOR="#000000">.</FONT><FONT COLOR="#0000ff">%Save</FONT><FONT COLOR="#000000">()
}</FONT>