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?
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?
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.
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
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
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.