How to write integer to text file

Bottom line, very technical all you rocket scientists but great background information to better understand the differences. Fortunately, although I need to learn correct coding the program I’m rewriting is a complete rewrite for a Home Automation System that has crunched away 24/7 for 15 plus years which needs less glorious code and error checking. It was originally written in the Real Basic 4.5 days and I haven’t coded since. Most likely appalling code not understanding opps structure at that time. I hope to take more time understanding classes and all the other crazy stuff like inheritance, casting, class interfaces, etc. I will have many questions for you brilliant coders, most of the time looking for a bailout with a code examples if I can squeeze it out of you.

Markus, you gave me the code I needed to make it work, I just experimented replacing the .bin file with a .txt and then saved variant data to pStings. I can now use a text editor to see the data. It seems to work but I’m open as to why I shouldn’t or any improvements to consider. Remember I’m the old guy trying to learn new tricks. Thanks to all.

This is where I’m at with a test using a BinaryStream and it seems to work:
WRITE

[code]Var value,value2,value3,value4 As variant
Var Bolval as Boolean = true

Value2 = “Cats and Dogs” Rem String
Value3 = 123456 Rem Integer
Value4 = BolVal Rem Boolean value

Value = Value2 + ", " +Value3 + ", " +Value4

Var path As FolderItem = SpecialFolder.Desktop.Child (“HC Prefs Folder 2”)

If path.Exists = False Then path.CreateFolder

Var file As FolderItem = path.Child( “Menu Prefs” )

Var b As BinaryStream = BinaryStream.Create(file,True) 'Big Endian by default

b.WritePstring(value)

b.Close
[/code]

READ

[code]Var value As variant

Var path As FolderItem = SpecialFolder.Desktop.Child (“HC Prefs Folder 2”)

If path.Exists = False Then path.CreateFolder

Var file As FolderItem = path.Child( “Menu Prefs” )

If file.Exists = False Then 'Return False 'no file here
Rem add code
end

Var b As BinaryStream = BinaryStream.Open(file) 'Big Endian by default

msgbox b.ReadPstring

b.Close[/code]

[quote=490860:@Clifford Coulter]If file.Exists = False Then 'Return False 'no file here
Rem add code
end[/quote]
This is bad.

Instead, use:

If Not file.Exists Then Return False // False may or may not be here, compiler will told you End

Why using Return ?

Because if not the code will continue to be executed and if the file does not exists, you will get an error.

A PString have a leading Integer and MsgBox does know nothing about it.
So, you may get a ? (white in a black losange) in your MsgBox.

http://documentation.xojo.com/api/deprecated/msgbox.html
says MsgBox is deprecated, use the replacement.

https://documentation.xojo.com/api/data_types/additional_types/pstring.html

In clear:
your can do what you want, but .txt files are made to hold … TEXT data, while binary files (what you want, created, etc. with BinaryStream) can hold what you want using a different set of commands and can store Binary Data (as I early wrote: Integer, Double, Float, etc.)

Remember:
What “works” may not be what have to be done. If you cannot open a binary file with TextEdit (macOS), there is a good reason.

Now, continue to do what you want.

PS: I know it is not easy.

After all this, what you are doing is saving 3 values in a CSV file.

The TextOutPut Stream is what you should be using.
You are ‘getting away with a lot’ by using variants to glue together strings and integers anyway.

A BinaryStream is what you use to create something like a PNG file or a structure… the kind of thing you don’t try to open in a text editor.

Your file holds
Cats and Dogs,123456,true

So (ignoring much discussion about if the file exists)

[code]Var documents As FolderItem = SpecialFolder.Documents
Var file As FolderItem = Documents.Child(“Sample.txt”) // or your file
var Valu as string

If file <> Nil Then
Try
Var output As TextOutputStream = TextOutputStream.Create(file)
Valu = Value2 + “,” + Value3.totext + “,” + value4.totext
output.WriteLine(Valu)
output.Close
Catch e As IOException
// handle
End Try

End If 'if not nil
[/code]

you not trust what xojo save because you look into the output file?
important for binary is little endian & big endian if different apps work together. the byte order. see .LittleEndian Propertie.
if you write a file you should add a file version info and check it at read.
It is possible that sooner or later a file format will be modified.

usually you would write this
b.WritePstring(value1)
b.WriteInt32(value2)
b.WriteBoolean(value3)
but i guess you know it and it was only a test with this single string.

I get your points Emile S. Noted: There may be legacy macOS APIs that make use of this data type, but otherwise, it should be avoided. OS 9 was the system it was originally written on and maybe that is why I was stuck on pstring. I will bite the bullet and use a .bin file and forget about the convenience of troubleshooting with a text editor or go back to TextOutPut Stream. The Return false thing was just a temp removal to make it work standalone. This was a very helpful Emile. Thanks

Jeff, I think I was stuck on using a BinaryStream since it worked fine before but I see no advantage now. I mostly use the files for storing sensor data from arrays and parameters. PS I like the Value3.totext convertion which is new to me.

You can use a BinaryStream, and if you use .Write to write a string to it (as in my example), then a string is what you will see if you examine the file with a hex editor.

Of course I did. Don’t be insulting.

None intended Tim.