Page 1 of 1

[Solved] Writing text to file without quotes

PostPosted: Tue Jul 08, 2014 5:20 pm
by Pax.Plastica
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.

Re: Writing text to file without quotes

PostPosted: Tue Jul 08, 2014 7:40 pm
by RPG
Hello

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

Romke

Re: Writing text to file without quotes

PostPosted: Tue Jul 08, 2014 10:08 pm
by Pax.Plastica
Thanks, that was it.

Re: [Solved] Writing text to file without quotes

PostPosted: Sun Nov 17, 2019 7:59 am
by DavidHMcCracken
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.

Re: [Solved] Writing text to file without quotes

PostPosted: Sun Nov 17, 2019 2:55 pm
by JeJe
You can also use the Put and Get statements.

Re: [Solved] Writing text to file without quotes

PostPosted: Mon Nov 18, 2019 5:27 am
by DavidHMcCracken
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.

Re: [Solved] Writing text to file without quotes

PostPosted: Mon Nov 18, 2019 12:50 pm
by JeJe
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



Re: [Solved] Writing text to file without quotes

PostPosted: Mon Nov 18, 2019 7:29 pm
by DavidHMcCracken
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.

Re: [Solved] Writing text to file without quotes

PostPosted: Mon Nov 18, 2019 7:33 pm
by RoryOF
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

Re: [Solved] Writing text to file without quotes

PostPosted: Mon Nov 18, 2019 9:56 pm
by JeJe
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


Re: [Solved] Writing text to file without quotes

PostPosted: Tue Nov 19, 2019 8:15 pm
by DavidHMcCracken
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.