Extract text file contents and put into string in xojo desktop app

As the title suggests, I’m having a hard time making my app take a look at a specific text file and then add its contents into an empty string. Any suggestions on how to make this happen would be appreciated.

Thank you

Take a look in the LR at TextInputStream http://documentation.xojo.com/index.php/TextInputStream

Look at the first example. Basically you could replace ‘TextArea1.Text’ in that example with the name of your variable.
What have you tried so far? :slight_smile:

By using open Dialog I know that I am getting to the right location and that my program knows where the file is but I can’t get it to open it on its own. I have to actually click on the file and open to see the content(just one line of text).

dim myFile As FolderItem
myFile = GetFolderItem("\folder\text file I want to open")
Dim d As OpenDialog
Dim t As TextInputStream
d = New OpenDialog
myFile = d.ShowModal
If myFile <> Nil Then
t = TextInputStream.Open(myFile)
TextArea1.Text = t.ReadAll
End If

What I want to be able to do is have the program open it without any user interaction and then copy the line of text in the text file into a string inside of the program.

What’s the actual path you’re using to the file?

Try something like this:

Dim f As FolderItem = GetFolderItem("absolute_path_to_file")
If f <> Nil Then
  If f.Exists Then
    // Be aware that TextInputStream.Open coud raise an exception
    Dim t As TextInputStream
    Try
      t = TextInputStream.Open(f)
      t.Encoding = Encodings.MacRoman
      TextArea1.Text = t.ReadAll
    Catch e As IOException
      t.Close
      MsgBox("Error accessing file.")
    End Try
  End If
End If

If the file never changes, you could just drag it into your project and access its contents as if it were a constant.

For example, if you drag the file “MyTextFile.txt” into the project, within your code you can do stuff like:

dim s as string = MyTextFile // no extension

I also don’t care about displaying the text in the textarea I was just using it to see if the program would actually access the file.

Just tried the code you suggested above(Albin) and nothing happens. I don’t get an error(msgBox) but the text file isn’t displayed in the textarea. I tried using similar code earlier and it didn’t work. Absolute path = c:\\practice\test1.txt

Kem- the application won’t have any kind of text area that is why I’m trying to find a way to open and copy content from text doc without outside interaction.

Thank you guys for your suggestions, I’ve been stuck on this for a couple of days now.

My example, didn’t make use of a TextArea, but you certainly could do something like:

TextArea1.Text = MyTextFile

As for Albin’s code, it looks perfect, so make sure the file actually exists. Trace the code to make sure it’s actually going to all the steps.

// // Copy of the code from above post by "Albin K" // with extra "Else" added. // // The sample file in this example is located at // "C:\\MyData\\WorkArea\\MyFile.txt" // // Supply the correct file name and its absolute path. // Dim f As FolderItem = GetFolderItem( "C:\\MyData\\WorkArea\\MyFile.txt" ) If f <> Nil Then If f.Exists Then // Be aware that TextInputStream.Open coud raise an exception Dim t As TextInputStream Try t = TextInputStream.Open(f) t.Encoding = Encodings.MacRoman TextArea1.Text = t.ReadAll t.Close Catch e As IOException t.Close MsgBox("Error accessing file.") End Try Else MsgBox("f does not Exist.") End If Else MsgBox("f is Nil.") End If

The app opens the file now!!! It looks like I was missing “c:” from my absolute path.

Thank you guys!! Y’all are awesome

I was able to figure out the last part on my own. I needed to move the content into a string so i used

dim s as string = textare1.text
msgbox(s) //To make sure that the command worked as I wanted it to

The message box shows the same as the text area. Now do you guys have any suggestions on how to do this without using the textarea?
What I mean by this is copy the content of the file into a string without displaying it into the text area, or using the textarea as an intermediate place.

If this is not possible I could always just make the text are invisible in the app so it’s not an issue. I’m just curious.

Thanks Again!!

Simply substitute the variable name for the textarea name.

TextArea1.Text = t.ReadAll

becomes

s = t.ReadAll

Thank you everyone. You have been a lot of help.