How to write integer to text file

I seem to be of the track to write a simple number to a text file. This is without any error checking.

Var b As BinaryStream

Var myFile As FolderItem = SpecialFolder.Desktop.Child (“HC Prefs Folder 2”).Child( “Menu Prefs” ) Rem my text file

b = BinaryStream.Open(myFile)

b.WriteSingle(RequestedBaudRate)

b.Close

This is not the way to do.

You save binary data using a BinaryStream.

Why ?
Text file hold text data (string). To save a number (integer, double, float, etc.) into a Text file, you would have to convert the data to Text, and at read time do the same (Read the text file and convert that value to numerics: Integer, DOuble, etc.)

Read the docs for more explanation.

… and do not forget to add a file extension to the target file.

Writing:

[code]Var documents As FolderItem = SpecialFolder.Documents
If documents <> Nil Then
Var file As FolderItem = Documents.Child(“Sample.txt”)
If file <> Nil Then
Try
// TextOutputStream.Create raises an IOException if it can’t open the file for some reason.
Var output As TextOutputStream = TextOutputStream.Create(file)

  output.Write(ConvertEncoding(str(nBaudRate), Encodings.UTF8))

// or possibly output.Write(nBaudRate.Totext )

  output.Close
Catch e As IOException
  // handle
End Try

End If
End If
[/code]

Read it back using a text input stream and convert what you read back into an integer

single is for values like this 12312.34534

b.WriteSingle(RequestedBaudRate)

.WriteInt32 should fit most needings

use .Create because for .Open the file must exists

binary is fast and without convertions. text is good if you like to look into and change it by editor.

dictionary & json is good for settings as TextStream.

Emile, is that not a Binary stream I am trying to create? Var b As Binary Stream. It seems that no mater what I try to place in the file, string or integer I receive a exception. The docs are a bit spotty for me at this point.

I can read the text file data if I edit the text file by hand before with this: b.Read(b.Length) but I cannot save anything yet. I receive exceptions on all. Please understand I am new to this.

---------- No good
b = BinaryStream.Open(myFile)
b.Write str(RequestedBaudRate) Rem exception happens
mainWindow.TextArea1.Value = b.Read(b.Length) Rem this line works
b.Close
--------------- No good

b = BinaryStream.Open(myFile)
b.Write(ConvertEncoding(str(RequestedBaudRate), Encodings.UTF8)) Rem exception happens
mainWindow.TextArea1.Value = b.Read(b.Length) Rem this line works
b.Close

------------ No good
b = BinaryStream.Open(myFile)
b.WriteInt32(RequestedBaudRate) Rem exception happens
mainWindow.TextArea1.Value = b.Read(b.Length) Rem this line works
b.Close

overwrite the file with .create not .open anything you made with editor.
what said the exception message?

you can write strings with .WritePString if you like to use strings in binary.

Every individual line after the Else statement will sill create and exception.

Var b As BinaryStream
Var myFile As FolderItem = SpecialFolder.Desktop.Child (“HC Prefs Folder 2”).Child( “Menu Prefs” )

if myFile = nil or not myFile.exists then
'File does not exist so create it
var LSC As LoadSaveClass
LSC = new LoadSaveClass

if LSC.OpenLoadDataFile(“Menu Prefs”) then
end
Else
b = BinaryStream.[b]Create/b
b.WriteInt32(RequestedBaudRate) Rem exception happens
mainWindow.TextArea1.Value = b.Read(b.Length) Rem this line works
'b.Close
End If

add a try catch and output this exception message. delete this file first by youself from desktop.
remark mainWindow.TextArea1.Value = b.Read(b.Length) Rem this line works
and not remark the close of the stream.
documentation.xojo.com/api/exceptions/ioexception.html

i made a video of file io (20 min) but is in german.
9. File IO

[quote]file input/output within the XOJO development enviroment.
text / binary / json / xml / examples / load menu + using the OpenFileDialog.[/quote]

I looked at the Video Markus and noticed you were using TextinputsStream or TextoutputStream and not a BinaryStream as I had intended. Somehow I did this back in the days of Realbasic 4.5 with a BinaryStream. Can anyone show me how to do this with a BinaryStream?

RequestedBaudRate = 13

Var TOS As TextOutputStream
Var myFile As FolderItem = SpecialFolder.Desktop.Child (“HC Prefs Folder 2”).Child( “Menu Prefs” )

if myFile = nil or not myFile.exists then
'File does not exist so create it
if LSC.OpenLoadDataFile(“Menu Prefs”) then
end
Else
TOS = TextOutputStream.Create(myFile)
TOS.WriteLine str(RequestedBaudRate)
TOS.Close
End If

Var TIS As TextInputStream
TIS = TextInputStream.Open(myFile)
mainWindow.TextArea1.Value = TIS.ReadLine
TIS.Close

Here’s what I do (for this post, I’ve left out all error checking). afn is the entire file path, image is read from an SQLite database (it’s a blob).

[code]Var afn As string, atFile As FolderItem, binstr As BinaryStream, image as MemoryBlock

atFile = new FolderItem (afn, FolderItem.PathModes.Native)
binstr = BinaryStream.Create (atFile, false)
binstr.write (image) // Write image out
binstr.flush ()
binstr.close () // Wrote the file out OK, close it now
[/code]

Haven’t had any problems so far.

its at minute 5 to 15

Thanks for your patience, Markus. What I did get from you works and I will use it until I understand how to do this with a BinaryStream. It did seem easier with BinaryStream when I wrote a program for my home automation system that is still running 24/7 in the Realbasic 4.5 days. it was not necessary to switch between input and output text streams and you had no string conversions to do. The old code I used doesn’t seem to work for me now. Maybe document files instead of a text file if that is actually different.

Tim I will try you suggestion.

Tim, I tried this basic test and received “An exception of class IOException was not handled. The application must shut down.”

Var RequestedBaudRate as Integer

RequestedBaudRate = 13

Var b As BinaryStream
Var myFile As FolderItem = SpecialFolder.Desktop.Child (“HC Prefs Folder 2”).Child( “Menu Prefs” )

if myFile = nil or not myFile.exists then
Msgbox “Create folders”
Else
b = BinaryStream.create(myFile, false)
b.write str(RequestedBaudRate) Rem wants a string, WHY?
Rem tried this also b.WriteInt32(RequestedBaudRate)
b.flush ()
b.Close ()
End If

Add a Return after:
Msgbox “Create folders”

The code continue its execution at the End If next line.

You have no error handling (nor exception handling) in your code.

You either do not read the LR links I gave or do not understand them.

You do not (or I do not saw) where the error occured.

Addition:
The answer is in the docs.

I didn’t notice any Links you sent but I have been looking at the docs for a couple of days. I was believing that the binarystream would convert the integers to a binary format and when read back in would then again convert the data to integers.

The return is academic in this example since the file does exist. At this point, I am only trying to save or read the data through a binary stream which may be the wrong to do it after all. Your right about error handling but for now unless it needs to be there to actually write and read the files I excluded it.

I’m sorry to be so slow understanding this but I’m trying. I do have it working with a text stream.

That is correct. I don’t know why you’re getting errors, but I’ve done this quite a bit. BinaryStream can write either binary data or text with no problem.

http://documentation.xojo.com/api/files/binarystream.html
http://documentation.xojo.com/api/exceptions/ioexception.html
http://documentation.xojo.com/api/files/binarystream.html#binarystream-create :

Notes

The IOException represents errors that can occur while using the shared methods for opening and creating streams: BinaryStream.Open, BinaryStream.Create, TextOutputStream.Create, TextOutputStream.Append, and TextInputStream.Open. If you are using older APIs, you need to check the FolderItem.LastErrorCode property.

Put a breakpoint at that line and check that “myFile” is not nil.
Then, if the file already exists (check its “exists” property), you’re expected to get an exception. You must use “BinaryStream.Create(myFile,true)” in order to not get an exception (and create the file) when it already exists.

Finally, use “step over” that line and check whether “b” is nil or not.

All this will give you several clues.

you should see the method parameters in status bar if you click or move the mouse at the method name.

add try catch around and output this Exception message somehow.

Arnaud Nicolet - The myfile did exist and still does at the speak break point. An exception then occurs at b.Create(myFile,true). Could this be a problem with how the file was created. Protected or something? What could I look at to tell me the file might have that issue.