[Solved] Writing text to file without quotes

Creating a macro - Writing a Script - Using the API (OpenOffice Basic, Python, BeanShell, JavaScript)
Post Reply
Pax.Plastica
Posts: 2
Joined: Tue Jul 08, 2014 5:14 pm

[Solved] Writing text to file without quotes

Post 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.
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
RPG
Volunteer
Posts: 2250
Joined: Tue Apr 14, 2009 7:15 pm
Location: Netherlands

Re: Writing text to file without quotes

Post 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
LibreOffice 7.1.4.2 on openSUSE Leap 15.2
Pax.Plastica
Posts: 2
Joined: Tue Jul 08, 2014 5:14 pm

Re: Writing text to file without quotes

Post by Pax.Plastica »

Thanks, that was it.
Apache OpenOffice 4.1.0 on Windows 7 and Windows XP
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Post 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.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writing text to file without quotes

Post by JeJe »

You can also use the Put and Get statements.
Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Post 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

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.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writing text to file without quotes

Post 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


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


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


Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Post 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.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
User avatar
RoryOF
Moderator
Posts: 34586
Joined: Sat Jan 31, 2009 9:30 pm
Location: Ireland

Re: [Solved] Writing text to file without quotes

Post by RoryOF »

There is discussion in this thread
viewtopic.php?f=20&t=87906
on setting a file to UTF-8
Apache OpenOffice 4.1.15 on Xubuntu 22.04.4 LTS
JeJe
Volunteer
Posts: 2764
Joined: Wed Mar 09, 2016 2:40 pm

Re: [Solved] Writing text to file without quotes

Post 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


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


	' 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

Windows 10, Openoffice 4.1.11, LibreOffice 7.4.0.3 (x64)
DavidHMcCracken
Posts: 44
Joined: Tue Apr 10, 2018 6:15 am

Re: [Solved] Writing text to file without quotes

Post 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.
W10 Libre 6.1.5.2 and Ubuntu-Mate 18.04 Libre 6.0.7.3
Post Reply