Read from a text file

Files have me totally buffaloed.

All I want to do is open and read from a text file that contains a series of comma delimited text lines. The filename is fixed (“wisparam.txt”) and there is no user interaction to select it - it is hardwired into the code. I can’t figure out how to open the file correctly so it can be read from.

I tried:

dim infile as folderitem
infile=“wisparam.txt”
dim input as TextInputStream
input=TextInputStream.open(InFile)

But I get an error that says Type mismatch. Expected class FolderItem, but got TextLiteral infile=”wisparam.txt”

In my old QBASIC programming days, I’d open a file just by:

open “wisparam.txt” for input as #1

Things are a lot more complicated in XOJO.

You are close. I remember QBASIC too. But Xojo starts to make sense after a while.

All of these do the same thing:

'option 1
var inFile as FolderItem
inFile = New FolderItem(“c:\abc\winsparam.txt”, FolderItem.PathModes.Native)

var ti as TextInputStream
ti= TextInputStream.Open(inFile)

'option 2
var inFile2 as New FolderItem(“c:\abc\winsparam.txt”, FolderItem.PathModes.Native)
var ti2 as TextInputStream = TextInputStream.Open(inFile2)

'option 3
var ti3 as TextInputStream = TextInputStream.Open(New FolderItem(“c:\abc\winsparam.txt”, FolderItem.PathModes.Native))

Hi @Gerald_Mahun - don’t get too discouraged (yet!) :wink:

Take a few minutes to scroll through FolderItem to get familiar with it. That should give you some ideas.

If your file is not changed in time (read only its contents ad vitam), do this:

Drop the file into your Navigation pane,
Use wisparam as a String variable when you need its contents.

This is like if you have created a String variable and filed it with the text file contents.

At compile time, your text file is included inside the Application.

special folder is also nice to use

= SpecialFolder.Documents.Child("wisparam.txt")

at TextInputStream use .ReadAll

there is a example at ReadLine

It’s just getting more confusing. My WisParam.TXT file is in the same folder as my sources code and when the application is installed, it will be placed in the same folder as the executable code, So I tried accessing the parameters file this way: Var wisparamFile As FolderItem =App.ExecutableFile.Parent.Child(“WisParam.TXT”)

When I test it, the software can’t find the file even though it is there. So I added MessageBox(wisparamFile.NativePath) to see the file’s path. It came back with.C:\Uasers\Me\AppData\Local\Temp\DebugConCoord\WisParam.TXT and of course that’s not where the file is. Why can’t I tell it to look explicitly in my source code folder instead of the Debug folder?

The thing that is really frustrating is if I install the parameters file in any folder besides the source code folder, I can easily access it. It’s only when I put it in the same folder with my code that Xojo goes debug-s**t crazy and refuses to access the file.

Never a good Idea to put files you will be working with in the executable folder, place them in something like
SpecialFolder.ApplicationData.Child("YourAppName")

Dealing with FolderItems to refer to files instead of Strings takes some getting used to. I hated it at first too, but have come to realize how necessary it is when dealing with files across operating systems. There’s also a lot of convenience there.

So the issue of your file not being where you think it should be could be because every time you Debug your application, Xojo clears out the old debug folder and makes it again.

Make sure under your “Build Settings” for the OS you’re on, you create a “Copy Files” step. Drag your WisParam.TXT into it and make sure it’s set to go to the App Parent Folder.

This way, when you click Run, your app will be compiled, your text file will be copied to the app’s folder, and App.ExecutableFile.Parent.Child(“WisParam.TXT”) will actually exist.

So you place your data file in Windows’s application folder ?

Really bad idea: follow previous entries for how to do that.

If that file is read-only you could simply import it into your project so it becomes part of the compiled app. If you plan to also write to it it is best placed in your application’s application folder (see Brian’s suggestion). If there is some initial content of the file you could combine both approaches: import the initial file into your project, copy that file to the application folder in the Opening event of your app if it doesn’t yet exist there, and then write to (and read from) that copy.

Nope.

App.ExecutableFile.Parent.Child

That path is different when running debug, compared to when it is running as compiled code.

Stick a messagebox just before to see what App.ExecutableFile.Parent

is actually giving you.

The right answer (as everyone says) is to put the file in

SpecialFolder.ApplicationData.Child("YourAppName")

You check if that is present when your app starts. If not, create it

Then check if the param file is present - if not then create that. (Perhaps copy a text file that is bundled with your app)

That allows people to manually edit the parameters if they choose later.

Only after those initial steps do you go to open the text file for use