What am I doing wrong!!!!

I must have a dose of the stupids tonight. The following snippet is copied from a working section of code, but when pasted onto the app I am working on it raises an IOException error;

[code]dim ln as string
dim fi as FolderItem
dim ti as TextInputStream
dim inpstr as string
fi=getfolderitem(“Products.txt”)

ti = TextInputStream.Open(fi) '<<<<this line raises the error
ln=ti.ReadLine
msgbox ln[/code]

‘Products.txt’ is a real file that is not locked and is located in the same folder that the app is in. What has really got me unnerved is that the code is from a working method with no modification.

[quote=116825:@chris benton]I must have a dose of the stupids tonight. The following snippet is copied from a working section of code, but when pasted onto the app I am working on it raises an IOException error;

[code]dim ln as string
dim fi as FolderItem
dim ti as TextInputStream
dim inpstr as string
fi=getfolderitem(“Products.txt”)

ti = TextInputStream.Open(fi) '<<<<this line raises the error
ln=ti.ReadLine
msgbox ln[/code]

‘Products.txt’ is a real file that is not locked and is located in the same folder that the app is in. What has really got me unnerved is that the code is from a working method with no modification.[/quote]

You know that if you run the project in the IDE, the debug executable is not in the same folder, right ?

#if DebugBuild f = getFolderItem("").Parent.Child("Products.txt") #else f = getFolderItem("Products.txt") #endif

Another way to proceed is to add a copyfile build step which copies Products.txt into the app parent folder.

At any rate, you should never open a textinputstream without checking if the folderitem is not nil, and that its Exists is true.

Hope this helps.

Thanks Michel, wanna swap brains? mine is old and doesnt like to look at help files.

Mine is vintage as well, but that old horse did learn a while ago the favorite programmer’s prayer : RTFM…

[quote=116839:@Michel Bujardet]Mine is vintage as well, but that old horse did learn a while ago the favorite programmer’s prayer : RTFM…

[/quote]
Very true philosophy, however knowing where to look is half the trick. BTW adding #if DebugBuild
f = getFolderItem("").Parent.Child(“Products.txt”)
#else
f = getFolderItem(“Products.txt”)
#endif
Didnt work, I am still getting the IOexception, Its Saturday night here, but ‘beer o’clock’ sounds better.

[quote=116842:@chris benton]Very true philosophy, however knowing where to look is half the trick. BTW adding #if DebugBuild
f = getFolderItem("").Parent.Child(“Products.txt”)
#else
f = getFolderItem(“Products.txt”)
#endif
Didnt work, I am still getting the IOexception, Its Saturday night here, but ‘beer o’clock’ sounds better.[/quote]

When you’re back from beer (tomorrow ? or Monday :wink: ) :

  • Click the menu ‘Insert’
  • Select ‘CopyFile’
  • In Copyfile1, drag Products.txt. In the inspector, make sure the destination is "Parent folder’
  • Drag CopyFile1 into the Windows build setting
  • Open the Windows build setting, and drag CopyFiles1 under the gear icon

When you run your project or build it, the file Products.txt is magically copied next to the executable, whether or not debug, so you will be able to access it through f = getFolderItem("Products.txt") (don’t use the #If debugbuild then).

Have a nice one.

I did what you said and still the same IO exception. here is the current code;

[code]dim ln as string
dim fi as FolderItem
dim ti as TextInputStream
dim inpstr as string
fi=getfolderitem(“Products.txt”)

ti = TextInputStream.Open(fi)’<<< this is where the trouble happens

ln=ti.ReadLine[/code]

[quote=116843:@Michel Bujardet]Click the menu ‘Insert’
Select ‘CopyFile’
In Copyfile1, drag Products.txt. In the inspector, make sure the destination is "Parent folder’
Drag CopyFile1 into the Windows build setting
Open the Windows build setting, and drag CopyFiles1 under the gear icon[/quote]

Does the above apply to a Mac running Xojo or to a Windows machine running Xojo? Im using a Mac

It applies to both.

But on Mac, it is kind of bad form to place a document next to the executable. Would you really litter your Applications folder with all sorts of files ? That is the reason why I thought you where using Windows.

Why not instead place your file in

fi=SpecialFolder.Documents.child("Products.txt")

That would be more in line with Mac OS X …

Thanks for the help Michel. I finally got the thing going by placing the copy files in the OSX build section. I still havent got to grips with the way OSX does file paths. On a PC I could just go ‘C:\applicationfolder\appsubfolder\filename’ On a Mac it seems to be much more convoluted.

If you want to get comfortable cross platform, I strongly suggest you stop referring to the command line path, which in Mac would simply be /Applications/Subfolder/Myapp.app by the way, so I do not see where the convolution could be. Learning a bit how works the Unix command line is not unnatural if you want to master the Mac. Each system has its own peculiarities.

Now, what will work on BOTH Windows and Mac is in Xojo code:

f = SpecialFolder.Applications.Child("SubFolder").Child("filename")

To get the path from that, it is as simple as

f.ShellPath

As usual Michel, I learn a ton of things everytime you post. Thank you

You are welcome, Chris. Glad my blabber serves :slight_smile:

If what you’re looking for is a file next to the application then it is quite possibly in a different location on OS X and Windows

Say you call your app “MyApp” and , for the sake of discussion, you put your app inside a directory on both OS X and Windows called MyApp.
So your “exe” is inside that directory
And we’ll assume you put your app in the “recommended location for applications” on each

On Windows you would then put your app in a directory, MyApp, in C:\Program Files
Your app then is C:\Program Files\MyApp\MyApp.exe
Your data file, called MyData, would be at C:\Program Files\MyApp\MyData

On OS X then you would put your app in /Applications/MyApp/
But in this case because of how OS X package applications your executable is actually in /Applications/MyApp/MyApp.app/Content/MacOSX/MyApp
(end users perceive /Applications/MyApp/MyApp.app as the runnable executable though)
And your data file NEXT TO the “exe”, or what users perceive as the exe, would be /Applications/MyApp/MyData

Now people can, and do, put applications in all kinds of places and not always in the recommended spot.
So to get the file “next to” your app on each platform regardless of where the application is you will need some code like

dim f as folderItem = app.ExecutableFile
On Windows now you have (for our example, f referring to C:\Program Files\MyApp\MyApp.exe) and the data file next to the exe is

dim nextToExe as folderitem = app.executableFile.Parent.child("MyData")

- parent is C:\\Program Files\\MyApp - child is C:\\Program Files\\MyApp\\MyData

But, as you can see from the above this won’t work on OS X because

dim f as folderItem = app.ExecutableFile
f refers to /Applications/MyApp/MyApp.app/Content/MacOSX/MyApp

dim nextToExe as folderitem = app.executableFile.Parent.child(“MyData”)

- parent is /Applications/MyApp/MyApp.app/Content/MacOSX - child is /Applications/MyApp/MyApp.app/Content/MacOSX/MyData

but really your data file is at /Applications/MyApp/MyData

To make this work x-platform you need something like

dim f as folderItem = app.ExecutableFile
dim nextToExe as folderitem
#if TargetMacOS
      nextToExe = app.executableFile.Parent.Parent.Parent.Parent.child("MyData")
#else
     nextToExe = app.executableFile.Parent.child("MyData")
#endif

And now this will find MyData beside the application executable on both platforms even if the application is NOT in the recommended location

Yet more great help, you guys should write the help files in Xojo