Loading something from a text file

Hey all,

My aim is in converting a project from VB6, and one thing I need to do is load a number from an existing text file into a variable. Here’s how I did it in VB6:

Open "svi.nan" For Input As #1 Input #1, svn Close #1

What is the Xojo equivalent of this? Or will this run in Xojo as intended?

The only thing in the file is a number?

Take a look at BinaryStream.

Yes, there’s just a single Integer number in the file. I want to read the number into a variable from a text file.

dim f as folderitem = getfolderitem(“svi.nan”)
dim t as textinputstream = textinputstream.open(f)
dim svn as integer = cdbl(t.readall)

Does the svi.nan file have to go in a certain place? I have it loaded into the project itself.

I also have the file itself in the folder where the project is located.

This line is giving me an IOException when it runs:

dim t as textinputstream = textinputstream.open(f)

I assume the file must be located in the folder on your hard drive where the project file itself was run from? The file is there, name is correct, yet the error code is telling me ‘File Not Found’. Can someone pinpoint if i’m missing something, please?

Hmm, If I use a hard-coded path in the IDE, it works ok, in this case:

dim f as folderitem = getfolderitem("C:\\Apps and Downloaded Stuff\\svi.txt")

Is there a way to code getfolderitem to use the current folder the app is running from, whether in the IDE or the compiled version?

See if this helps.

//
// Get the Application folder
//
Dim f As FolderItem
Dim strPath As String
f=GetFolderItem ("")
strPath=f.AbsolutePath

On Windows, when you run from the IDE, the project is built in a new subfolder, so your file won’t be next to the program. Try

#if DebugBuild
   f = getFolderItem("").Parent.Child("svi.nan")
#else
   f = getFolderItem("svi.nan")
#endif

[quote=80364:@Derek DiBenedetto]Does the svi.nan file have to go in a certain place? I have it loaded into the project itself.

I also have the file itself in the folder where the project is located.

[/quote]

If you drag a file into your project, it becomes a constant string, which you can use by the name it has in the project. For instance “svi”.

See other posts for how to load a file placed in your app directory.

Thanks, Tim…your suggestion works fine!

A side question to this: In VB6, I used a text file to save (global) variables for saved games, this way:

Open "sag.txt" For Output As #1 Write #1, nm, mn, dy, yr Write #1, cash, influ, byt, exp

Then when the player loaded the game, I used this:

Open "sag.txt" For Input As #1 Input #1, nm, mn, dy, yr Input #1, cash, influ, byt, exp

I know now how to load a single item from a text file, but would the procedure be different for a set of variables like this? What would the code be to do the above? Using the latest version 2014r1.1, Windows PC.

You have to write or read each value individually. Take a look at BinaryStream.

http://documentation.xojo.com/index.php/BinaryStream

So, I would use the code above to find or create the text file, then use this to save the variable, for example:

stream = BinaryStream.Create(f, True) // Overwrite if exists stream.Write(nm) stream.Close

for each variable? Is there a way to save multiple items at once so that they can loaded separately later? I’ll have about 40 variables I use throughout the game, so this would be rather tedious to save one by one.

[quote=80459:@Derek DiBenedetto]So, I would use the code above to find or create the text file, then use this to save the variable, for example:

stream = BinaryStream.Create(f, True) // Overwrite if exists stream.Write(nm) stream.Close

for each variable? Is there a way to save multiple items at once so that they can loaded separately later? I’ll have about 40 variables I use throughout the game, so this would be rather tedious to save one by one.[/quote]
No don’t open, write & close the file for each variable - you’ll wipe it our each time
Open it once - write as much stuff as you need (which is what your vb did just with one line) then close it

stream = BinaryStream.Create(f, True) // Overwrite if exists
stream.Write(nm)
stream.Write(nm)
stream.Write(nm)
stream.Write(nm)
stream.Write(nm)
stream.Write(nm)
stream.Close

Reading it is similar - open it read as much as you need close it

Again, sort of related to this issue:

I’m trying to read a text file with a list of street names, each on their own line in the file. Here’s the code i’m using now:

Dim f as FolderItem #if DebugBuild f = getFolderItem("").Parent.Child("streets.txt") #else f = getFolderItem("streets.txt") #endif dim t as textinputstream = textinputstream.open(f) Dim snstore(1319) as String Dim z as Integer Dim x as Integer lr1: z = z + 1 If z = 1320 Then GoTo elr1 snstore(z) = t.readall GoTo lr1 elr1:

This reads in the first name in the list fine, then the rest of the list is blank. Can someone please let me know what i’m doing wrong and correct my code, if possible?

First of all, you don’t need to pre-allocate the array. In Xojo you can append to an existing array. So, assuming that you don’t need the error checking for a fixed number of entries, you can write it like this.

  Dim f as FolderItem
  #if DebugBuild
    f = getFolderItem("").Parent.Child("streets.txt")
  #else
    f = getFolderItem("streets.txt")
  #endif
  dim t as textinputstream = textinputstream.open(f)
  Dim snstore() as String
  while not t.EOF
      snstore.Append t.ReadLine
  wend

ReadLine reads the file one line at a time.

Thanks, Tim, works great! And actually much less verbose and more elegant than the method I used with VB as well. Very cool.

Also, don’t use GoTo as it leads to hard-to-read code and hard-to-find bugs. Use one of the existing loop structures instead: while … wend, do [until] … loop [until], or for [each] … next. If you need to leave a loop early, you can use exit.