HOW DO I WRITE "Stream.GlobalCharacter" TO A FILE

294 views
Skip to first unread message

Brad Harum

unread,
Jun 6, 2013, 1:54:52 PM6/6/13
to Ensemble-in...@googlegroups.com
HI ALL,

I need to write a stream out to a file and I keep getting errors from the classes I am trying.  Can anyone please respond with some basic syntax on how to write from a "Stream.GlobalCharacter" to a file please.

Thanks in advance.

Brad

Rodolfo Mendes

unread,
Jun 6, 2013, 2:20:27 PM6/6/13
to Ensemble-in...@googlegroups.com
Hi Brad,

Tell me if this helps you:

Set tOldStream = ##class(%Stream.GlobalCharacter).%New()
Set tStatus = tOldStream.Write("abc")
Quit:('tStatus)

Set tNewStream = ##class(%Stream.FileCharacter).%New()
Set tNewStream.Filename = "<absolute path to output file>"

Set tStatus = tNewStream.CopyFrom(tOldStream)
Quit:('tStatus)

Set tStatus = tNewStream.%Save()

Regards,

Rodolfo Mendes




Brad

--
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare Community" group.
To post to this group, send email to Ensemble-in...@googlegroups.com
To unsubscribe from this group, send email to Ensemble-in-Healt...@googlegroups.com
For more options, visit this group at http://groups.google.com/group/Ensemble-in-Healthcare?hl=en
---
You received this message because you are subscribed to the Google Groups "InterSystems: Ensemble in Healthcare" group.
To unsubscribe from this group and stop receiving emails from it, send an email to Ensemble-in-Healt...@googlegroups.com.
For more options, visit https://groups.google.com/groups/opt_out.
 
 

Lawrence Harris

unread,
Jun 6, 2013, 2:26:11 PM6/6/13
to <Ensemble-in-Healthcare@googlegroups.com>
Here is a simple file writer.  Others are very similar.

If you have a %Stream.GlobalCharacter and you want to write to a file use the copy method on the file in place of the .Write and .WriteLine in the example.

objFile.CopyFrom(stmInputData) where stmInputData is your %GlobalCharacterStream.

The class/method:
Class LHH.TestStream.Main Extends %Persistent
{

ClassMethod TestFile(
strDirectory As %String,
strFilename As %String) As %Status
{
Set sc=$$$OK
Set strDirectory=##class(%File).NormalizeDirectory(strDirectory)
Set strFullPath=strDirectory_strFilename
Set strFilename=##class(%File).NormalizeFilename(strFullPath)
Set objFile=##class(%FileCharacterStream).%New()
Write "Full path = ",strFullPath,!
Set objFile.Filename=strFullPath
If '$IsObject(objFile) {
If $Data(%objlasterror) Quit %objlasterror
Quit $$$ERROR($$$GeneralError,"Failed to open file")
}
Do objFile.Write("Hi there")
Do objFile.Write(" and more")
Do objFile.WriteLine(" and the end")
Set sc=objFile.%Save()
If $$$ISERR(sc) Write $system.Status.GetErrorText(sc),!
Else  Write "Ok",!
Quit sc
}

}

Run the class:
>s sc=##class(LHH.TestStream.Main).TestFile("U:\lharris\test\","testfile.txt")

Output File:
-----------------------------
Hi there and more and the end
-----------------------------

Lawrence Harris
activEHR
by EMRlogic Systems

Ted Peck

unread,
Jun 6, 2013, 3:15:24 PM6/6/13
to Ensemble-in...@googlegroups.com, Brad Harum
This should work:

Set file=##class(%IO.FileStream).%New()
Do file.Open(filename,"NEW")
Do globalstream.Rewind()
Do file.CopyFrom(globalstream)
Kill file
Reply all
Reply to author
Forward
0 new messages