Reading text file not working

Where am I going wrong. I want to read “textfile.txt”. If I hardcode it, it craps out.

Currently the below pops up a requestor and I select the file, reads correctly. If I force it and tell it the specific file, it fails at textinputstream.open.

Where am I going wrong?
Thanks!

var f As FolderItem
Var textInput As TextInputStream
Var rowFromFile As String

'f= specialfolder.desktop.child(“textfile.txt”)

f = FolderItem.ShowOpenFileDialog(“text/plain”)
If f <> nil and f.exists Then
textInput = TextInputStream.Open(f)
textInput.Encoding = Encodings.UTF8
Do
rowFromFile = textInput.ReadLine
if rowFromFile.left(6) = “dbloc=” then
gdbLocation = rowFromFile.right(len(rowfromfile) - 6)
msgbox(gdbLocation)
end if
Loop Until textInput.EndOfFile
textInput.Close
else
msgbox(“File not found.”)
End If

If you’re on macOS it may be because your app does not have permission to access the Desktop.

Part of the Apple’s security standards are that developers must request file access from the user using an open or save dialog. The only location you’re allowed access to without permission is SpecialFolder.ApplicationData.Child("com.yourbundle.identifier")

@Sam_Rowlands reports that on newer macOS versions, SpecialFolder.Temporary may not always be accessible.

Its Windows based.

Use the code shared in TextInputStream and add inside the read process code to achiev your goad.

It miss this:
Try
Catch
End Try

docs.xojo.com/TextInputStream example:

Var f As FolderItem = FolderItem.ShowOpenFileDialog(“text”) // as defined in File Type Sets Editor
If f <> Nil Then
If f.Exists Then
// Be aware that TextInputStream.Open could raise an exception
Var t As TextInputStream
Try
t = TextInputStream.Open(f)
t.Encoding = Encodings.UTF8
TextArea1.Value = t.ReadAll
Catch e As IOException
MessageBox(“Error accessing file.”)
End Try
t.Close
End If
End If

Also, avoid MsgBox; use instead (for degugging):

System.DebugLog gdbLocation

and read it on real time while the code executes… (if you have the reporting window part visible.

Emile:

No offense, but that didn’t help any. My code works if I pop upba requestor. If I hardcode it fails at textinputstream.open. adding try and the other stuff isnt needed for my example.

What fails?

1 Like

No offense.

It could help.

Also, I think I recall something like FolderItem isReadable / Permissions:

It can be useful in the example while you are in debug mode.

Fails to open the file as stated in original post.

What I’m trying to do is open a specific file without asking for the end-user to select the file with a requestor.

Are you getting an exception? What does it say?

All:

Has there ever been a time where you’re reading docs and know you’ve read it correctly but no matter what you do you’re wrong? Then you realize PEBKAC?

My filename was misspelled. Sigh.

1 Like

Mark it as the anwer…:wink:

LOL! ok

1 Like