You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@openoffice.apache.org by Johnny Rosenberg <gu...@gmail.com> on 2013/11/23 00:41:56 UTC

Basic Macro – write a file

Dim iNumber As Integer
iNumber = Freefile
Open sFilePath For Output As #iNumber
Print #iNumber, "Line 1"
Print #iNumber, "Line 2"
Close #iNumber


The file now looks like this (in hex values):
4C 69 6E 65 20 31 0A
4C 69 6E 65 20 32 0A

So every line ends with 0A (Chr(10)).
The file is going to be in a special text file format suited for a specific
app, so I need every line to end with 0D 0A (Chr(13) & Chr(10)). Is that
possible? How? Can I see a short example of that, that creates a text file
that looks like the following?
4C 69 6E 65 20 31 0D 0A
4C 69 6E 65 20 32 0D 0A



Regards

Johnny Rosenberg

Re: Basic Macro – write a file

Posted by Girvin Herr <gi...@sbcglobal.net>.
Johnny,
Looks like your file was created on a *nix machine, which uses only a 
linefeed (0x0a) as a line terminator.  What you want is a DOS/Windows 
line terminator of carriage return-linefeed (0x0d 0x0a).
I use dos2unix to convert such files:
     http://sourceforge.net/projects/dos2unix/
dos2unix is actually two programs, or filters.  One changes CR-LF to LF 
(dos2unix) and the other changes LF to CR-LF (unix2dos). Although I use 
Linux, this package uses stdin and stdout (redirection is implied), so 
it should compile and run with any machine that has a C compiler.

Hope this helps.
Girvin Herr



On 11/22/2013 03:41 PM, Johnny Rosenberg wrote:
> Dim iNumber As Integer
> iNumber = Freefile
> Open sFilePath For Output As #iNumber
> Print #iNumber, "Line 1"
> Print #iNumber, "Line 2"
> Close #iNumber
>
>
> The file now looks like this (in hex values):
> 4C 69 6E 65 20 31 0A
> 4C 69 6E 65 20 32 0A
>
> So every line ends with 0A (Chr(10)).
> The file is going to be in a special text file format suited for a specific
> app, so I need every line to end with 0D 0A (Chr(13) & Chr(10)). Is that
> possible? How? Can I see a short example of that, that creates a text file
> that looks like the following?
> 4C 69 6E 65 20 31 0D 0A
> 4C 69 6E 65 20 32 0D 0A
>
>
>
> Regards
>
> Johnny Rosenberg
>


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@openoffice.apache.org
For additional commands, e-mail: users-help@openoffice.apache.org


Re: Basic Macro – write a file

Posted by Johnny Rosenberg <gu...@gmail.com>.
2013/11/26 Andrew Douglas Pitonyak <an...@pitonyak.org>

> On 11/25/2013 12:32 PM, Johnny Rosenberg wrote:
>
>> That was the kind of solution I was looking for, actually… Since it
>> turned out that I didn't need the Windows style newline after all (I tested
>> with the Unix style newline instead and it worked fine), I don't need this
>> code, but SimpleFileAccess and TextOutputStream seems interesting!
>>
>  Based on my testing while writing the first edition of OOME, I decided
> that the general file reading and writing capabilities in StarBasic are
> very poor.


Yes, and I think there are some nasty bugs involved as well, especially
when in random access mode, if I recall correctly.


> You are much better off using the streams for input and output.


I guess you're right.



Johnny Rosenberg


>
>
> --
> Andrew Pitonyak
> My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
> Info:  http://www.pitonyak.org/oo.php
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: users-help@openoffice.apache.org
>
>

Re: Basic Macro – write a file

Posted by Andrew Douglas Pitonyak <an...@pitonyak.org>.
On 11/25/2013 12:32 PM, Johnny Rosenberg wrote:
> That was the kind of solution I was looking for, actually… Since it 
> turned out that I didn't need the Windows style newline after all (I 
> tested with the Unix style newline instead and it worked fine), I 
> don't need this code, but SimpleFileAccess and TextOutputStream seems 
> interesting! 
Based on my testing while writing the first edition of OOME, I decided 
that the general file reading and writing capabilities in StarBasic are 
very poor. You are much better off using the streams for input and output.

-- 
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
Info:  http://www.pitonyak.org/oo.php


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@openoffice.apache.org
For additional commands, e-mail: users-help@openoffice.apache.org


Re: Basic Macro – write a file

Posted by Johnny Rosenberg <gu...@gmail.com>.
2013/11/24 Andrew Douglas Pitonyak <an...@pitonyak.org>

> On 11/22/2013 06:41 PM, Johnny Rosenberg wrote:
>
>> Dim iNumber As Integer
>> iNumber = Freefile
>> Open sFilePath For Output As #iNumber
>> Print #iNumber, "Line 1"
>> Print #iNumber, "Line 2"
>> Close #iNumber
>>
>>
>> The file now looks like this (in hex values):
>> 4C 69 6E 65 20 31 0A
>> 4C 69 6E 65 20 32 0A
>>
>> So every line ends with 0A (Chr(10)).
>> The file is going to be in a special text file format suited for a
>> specific
>> app, so I need every line to end with 0D 0A (Chr(13) & Chr(10)). Is that
>> possible? How? Can I see a short example of that, that creates a text file
>> that looks like the following?
>> 4C 69 6E 65 20 31 0D 0A
>> 4C 69 6E 65 20 32 0D 0A
>>
>>
>>
>> Regards
>>
>> Johnny Rosenberg
>>
>>  Sadly, the obvious solution fails horribly by stripping the 0x0D
>
> Print #iNumber, "Line 1" & CHR$(13)
>
>
> This solution, however, works just fine!
>
>   Dim sFilePath$ : sFilePath = "/home/MrRosenberg/x.txt"
>   Dim oSFA        ' SimpleFileAccess service.
>   Dim sFileName$  ' Name of file to open.
>   Dim oStream     ' Stream returned from SimpleFileAccess.
>   Dim oTextStream ' TextStream service.
>   Dim sStrings    ' Strings to test write / read.
>   Dim sInput$     ' The string that is read.
>   Dim s$          ' Accumulate result to print.
>   Dim i%          ' Index variable.
>
>   sStrings = Array("One", "UTF:Āā", "1@3")
>
>   ' File to use.
>   sFileName = sFilePath
>   ' Create the SimpleFileAccess service.
>   oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
>   'Create the Specialized stream.
>   oTextStream = CreateUnoService("com.sun.star.io.TextOutputStream")
>
>   'If the file already exists, delete it.
>   If oSFA.exists(sFileName) Then
>     oSFA.kill(sFileName)
>   End If
>
>   ' Open the file for writing.
>   oStream = oSFA.openFileWrite(sFileName)
>
>   ' Attach the simple stream to the text stream.
>   ' The text stream will use the simple stream.
>   oTextStream.setOutputStream(oStream)
>
>   ' Write the strings.
>   oTextStream.writeString("Line 1" & CHR$(13) & CHR$(10))
>   oTextStream.writeString("Line 2" & CHR$(13) & CHR$(10))
>
>   ' Close the stream.
>   oTextStream.closeOutput()
>
> --
> Andrew Pitonyak
> My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
> Info:  http://www.pitonyak.org/oo.php
>
>
That was the kind of solution I was looking for, actually…
Since it turned out that I didn't need the Windows style newline after all
(I tested with the Unix style newline instead and it worked fine), I don't
need this code, but SimpleFileAccess and TextOutputStream seems interesting!

Thanks!


Johnny Rosenberg

>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe@openoffice.apache.org
> For additional commands, e-mail: users-help@openoffice.apache.org
>
>

Re: Basic Macro – write a file

Posted by Andrew Douglas Pitonyak <an...@pitonyak.org>.
On 11/22/2013 06:41 PM, Johnny Rosenberg wrote:
> Dim iNumber As Integer
> iNumber = Freefile
> Open sFilePath For Output As #iNumber
> Print #iNumber, "Line 1"
> Print #iNumber, "Line 2"
> Close #iNumber
>
>
> The file now looks like this (in hex values):
> 4C 69 6E 65 20 31 0A
> 4C 69 6E 65 20 32 0A
>
> So every line ends with 0A (Chr(10)).
> The file is going to be in a special text file format suited for a specific
> app, so I need every line to end with 0D 0A (Chr(13) & Chr(10)). Is that
> possible? How? Can I see a short example of that, that creates a text file
> that looks like the following?
> 4C 69 6E 65 20 31 0D 0A
> 4C 69 6E 65 20 32 0D 0A
>
>
>
> Regards
>
> Johnny Rosenberg
>
Sadly, the obvious solution fails horribly by stripping the 0x0D

Print #iNumber, "Line 1" & CHR$(13)


This solution, however, works just fine!

   Dim sFilePath$ : sFilePath = "/home/MrRosenberg/x.txt"
   Dim oSFA        ' SimpleFileAccess service.
   Dim sFileName$  ' Name of file to open.
   Dim oStream     ' Stream returned from SimpleFileAccess.
   Dim oTextStream ' TextStream service.
   Dim sStrings    ' Strings to test write / read.
   Dim sInput$     ' The string that is read.
   Dim s$          ' Accumulate result to print.
   Dim i%          ' Index variable.

   sStrings = Array("One", "UTF:Āā", "1@3")

   ' File to use.
   sFileName = sFilePath
   ' Create the SimpleFileAccess service.
   oSFA = CreateUnoService("com.sun.star.ucb.SimpleFileAccess")
   'Create the Specialized stream.
   oTextStream = CreateUnoService("com.sun.star.io.TextOutputStream")

   'If the file already exists, delete it.
   If oSFA.exists(sFileName) Then
     oSFA.kill(sFileName)
   End If

   ' Open the file for writing.
   oStream = oSFA.openFileWrite(sFileName)

   ' Attach the simple stream to the text stream.
   ' The text stream will use the simple stream.
   oTextStream.setOutputStream(oStream)

   ' Write the strings.
   oTextStream.writeString("Line 1" & CHR$(13) & CHR$(10))
   oTextStream.writeString("Line 2" & CHR$(13) & CHR$(10))

   ' Close the stream.
   oTextStream.closeOutput()

-- 
Andrew Pitonyak
My Macro Document: http://www.pitonyak.org/AndrewMacro.odt
Info:  http://www.pitonyak.org/oo.php


---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@openoffice.apache.org
For additional commands, e-mail: users-help@openoffice.apache.org