[Solved] Writing text to file without quotes

Creating a macro - Writing a Script - Using the API

[Solved] Writing text to file without quotes

Postby Pax.Plastica » Tue Jul 08, 2014 5:20 pm

I am probably missing something simple here, but I would like to use an OpenOffice Basic macro to write text to a file, but without the double quotes.
I want the file to look like this:

Line 1
Line 2

not like this:

"Line 1"
"Line 2"

Probably something simple, but I can't seem to get it done.
Last edited by Hagar Delest on Tue Jul 08, 2014 10:46 pm, edited 1 time in total.
Reason: tagged [Solved].
Apache OpenOffice 4.1.0 on Windows 7 and Windows XP
Pax.Plastica
 
Posts: 2
Joined: Tue Jul 08, 2014 5:14 pm

Re: Writing text to file without quotes

Postby RPG » Tue Jul 08, 2014 7:40 pm

Hello

It is not so clear what code you use but it is possible you have to use print in stead of write.

Romke
LibreOffice 6.2.5.2 on openSUSE Leap 15
RPG
Volunteer
 
Posts: 2176
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Writing text to file without quotes

Postby Pax.Plastica » Tue Jul 08, 2014 10:08 pm

Thanks, that was it.
Apache OpenOffice 4.1.0 on Windows 7 and Windows XP
Pax.Plastica
 
Posts: 2
Joined: Tue Jul 08, 2014 5:14 pm

Re: [Solved] Writing text to file without quotes

Postby DavidHMcCracken » Sun Nov 17, 2019 7:59 am

I know this is an old discussion but, if you are still following this, thank you RPG. I was tearing my hair out over this and your simple suggestion solved it.
OpenOffice 4.1.5 on Ubuntu 16.04
DavidHMcCracken
 
Posts: 19
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Postby JeJe » Sun Nov 17, 2019 2:55 pm

You can also use the Put and Get statements.
Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 623
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writing text to file without quotes

Postby DavidHMcCracken » Mon Nov 18, 2019 5:27 am

Thank you for your suggestion JeJe but I'm having trouble with Get (I haven't tried Put). For context, I have a plain text configuration file with each item on one line. I want to read this entire file into an edit control in a dialog for the user to examine and possibly modify. The only way that I have found to fill the edit is to read each line from the file, concatenating them (adding newlines) into a string and then assigning this to the edit.Text. The multi-line string is read from the file by:
Code: Select all   Expand viewCollapse view
fn = FreeFile()
open fspec for input as #fn
do while not EOF(#fn)
    line input #fn, oneline
    all = all & oneline & chr(13)
loop
close #fn

get #fn, var elicits run-time error "Action not supported. Invalid procedure call". I considered that the file might need to be opened differently, but the only form that doesn't delete the contents is "for input", which would make get useless, so I don't think that is a solution. I couldn't find any way to write the file back out until I saw the print suggestion. It writes out the entire contents of the edit control in one simple statement. From C and Python I'm used to seeing more symmetry in situations like this but I'm not sure it's there.
OpenOffice 4.1.5 on Ubuntu 16.04
DavidHMcCracken
 
Posts: 19
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Postby JeJe » Mon Nov 18, 2019 12:50 pm

Example put and get saving/loading unicode string to file - assumes file is smaller than the max size for strings.

Edit:note a Windows path name is used - change as appropriate.
Code: Select all   Expand viewCollapse view

sub testbinary
   Dim f As Integer,b() As Byte,st as string
   f=FreeFile
   
   '&HFEFF unicode byte order mark
   st = chr(&HFEFF) &  "Chess pieces:" & "♚ ♛ ♜ ♝ ♞ ♟ ♔ ♕ ♖ ♗ ♘"

   pth = "C:\tmp\a.txt"
   KILL PTH

   B= ST 'convert string to byte array and save file
   Open pth For binary access write As #f
   put #f,,b
   Close #f


   st = ""
   redim b(filelen(pth) -1) 'redim byte array to size of file
   Open pth For binary access read As #f
   get #f,,b
   Close #f
   st =b 'convert to string
   
   msgbox st
end sub




With binary access you can store any variable - not just strings. You can also store arrays. Use of a string array can get round the size limitation above.

The following subs save/load a string array with a long added at the start of the file specifying the size of the array - to facilitate reopening when you don't know the size of the array in advance)

Code: Select all   Expand viewCollapse view

Sub SaveStringArrWithUB(pth,sts())
   dim a as long
   a = ubound(sts)
   open pth for binary as #1
   put #1,,a
   put #1,,sts
   close #1
End Sub

Sub OpenStringArrWithUB(pth,sts())
   dim a as long
   if dir(pth,0)<>"" then
      open pth for binary as #1
      get #1,,a
      redim sts(a)
      get #1,,sts
      close #1
   end if
End Sub


Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 623
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writing text to file without quotes

Postby DavidHMcCracken » Mon Nov 18, 2019 7:29 pm

Thank you JeJe for your interesting and informative suggestions. I tried reading the file as binary and it did work as you indicate but all of the text showed up as Kanji. The file is written by external plain text editors and I imagine that it is UTF8.
OpenOffice 4.1.5 on Ubuntu 16.04
DavidHMcCracken
 
Posts: 19
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Postby RoryOF » Mon Nov 18, 2019 7:33 pm

There is discussion in this thread
https://forum.openoffice.org/en/forum/viewtopic.php?f=20&t=87906
on setting a file to UTF-8
Apache OpenOffice 4.1.7 on Xubuntu 18.04.3 (mostly 64 bit version) and very infrequently on Win2K/XP
User avatar
RoryOF
Moderator
 
Posts: 29861
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Writing text to file without quotes

Postby JeJe » Mon Nov 18, 2019 9:56 pm

It may be a plain ANSI file, in which case you need to use strconv to convert to and from an OOBasic (unicode) string.

Code: Select all   Expand viewCollapse view

sub getAnsiFileString(pth as string,st as string)

   dim b() as byte,f,flen as long

   if dir(pth,0) <>"" then
      f=freefile

      flen = filelen(pth)

      if flen<>0 then

         redim b(flen -1) 'redim byte array to size of file
         Open pth For binary access read As #f
         get #f,,b
         Close #f
         st = b
         st =strconv(st,64) 'convert to string
      end if
   end if
end sub


sub setAnsiFileString(pth as string,st as string)

   dim b() as byte,f,flen as long
   f=freefile

   if dir(pth,0) <>"" then
      KILL PTH
   end if
   B= strconv(st,128)  'from unicode

   Open pth For binary access write As #f
   put #f,,b
   Close #f

end sub





Unicode functions:

Code: Select all   Expand viewCollapse view

   ' unicode text files
sub setUnicodeFileString(pth as string,st as string)

   dim b() as byte,f,flen as long
   f=freefile

   if dir(pth,0) <>"" then
      KILL PTH
   end if

   if left(st,1) <> chr(&HFEFF) then st = chr(&HFEFF)& st
   B= ST 'convert string to byte array and save file
   Open pth For binary access write As #f
   put #f,,b
   Close #f

end sub

sub getUnicodeFileString(pth as string,st as string)

   dim b() as byte,f,flen as long

   if dir(pth,0) <>"" then
      f=freefile

      flen = filelen(pth)

      if flen<>0 then

         redim b(flen -1) 'redim byte array to size of file
         Open pth For binary access read As #f
         Seek #f,1
         get #f,,b
         Close #f
         st =b 'convert to string
      end if
   end if
end sub

Sub AppendUnicodeStringToFile(pth As String,st As String)
   Dim f As Integer,flen As Long
   flen = FileLen(pth)
   Dim b() As Byte
   b =st
   f=FreeFile
   Open pth For binary access write As #f
   Seek #f,flen +1
   Put #f,,b
   Close #f
End Sub

Openoffice 4.1.2
Windows 8
JeJe
Volunteer
 
Posts: 623
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writing text to file without quotes

Postby DavidHMcCracken » Tue Nov 19, 2019 8:15 pm

Thank you JeJe for both the strconv suggestion, which worked, and for your overall explanations and examples. You have not only given me a good solution to the read/write symmetry issue but taught me quite a few related things for which I will find additional uses.
OpenOffice 4.1.5 on Ubuntu 16.04
DavidHMcCracken
 
Posts: 19
Joined: Tue Apr 10, 2018 6:15 am


Return to Macros and UNO API

Who is online

Users browsing this forum: No registered users and 5 guests