If you want to manually control the contents of the file (outside of the game) and treat it as a normal .txt file, you don't use the Read/Write(Integer|Float|String). Those work in a binary file mode, not a plain text mode.
Plain text files use Write/ReadString.
Here are the functions I currently use for plain text files (some are commented better than others):
function FloatVal(a$)
// declare some bits
c AS integer
l AS integer
fd AS integer
dp AS integer
// initialise values
fd = 0
l = len(a$)
// search for the first dot in the string
for c = 1 to l
if mid(a$, c, 1) = "."
// indicate found
fd = 1
// store the location
dp = c
// get out of the loop
exit
endif
next c
// if no period found, just return float version of
// the value of the string
if fd = 0
v# = val(a$)
exitfunction v#
endif
// get the fractional part
fp$ = right(a$, l - dp)
// check for max
flen = len(fp$)
if flen > 7
fp$ = left(fp$,7)
flen = 7
endif
// calculate the divisor needed to move the decimal
div# = (10.0 ^ flen)
// convert, calculate and combine them
p1# = val(left(a$, dp)) * 1.0
p2# = (val(fp$) / div#)
v# = p1# + p2#
endfunction v#
remStart
Global variables used by the two file functions, place these next three in main.agc
global ta_aLine AS string
global dim ta_aSet[2] AS string
global ta_asInd AS integer
global ta_asCnt AS integer
global oops_source AS string
We are using globals because the to file functions are returning
either 1 or 0 to indicate if something was succesfully found.
The functions handle closing the file when the end has been reached.
The purpose of that is to be able to do a loop that parses
the file like this:
// read the file
while ta_getNextLine(i_file) = 1
// do something with the value in ta_aLine
endwhile
RemEnd
RemStart
Name: ta_trimString
Variables:
instr$ - string to trim
Output:
original string with all leading and trailing spaces removed
Description:
Remove all leading and trailing spaces in a string
RemEnd
function ta_trimString(instr$)
// initialise
restr$ = instr$
// get initial length
sLen = len(restr$)
// check for done
if sLen = 0 then exitfunction restr$
// trim the right end
while mid(restr$,sLen,1) = " "
// drop the last character
restr$ = left(restr$,sLen-1)
// reset length
sLen = len(restr$)
// check for done
if sLen = 0 then exitfunction restr$
endwhile
// trim from the left
while mid(restr$,1,1) = " "
// drop the last character
restr$ = right(restr$,sLen-1)
// reset length
sLen = len(restr$)
// check for done
if sLen = 0 then exitfunction restr$
endwhile
endfunction restr$
RemStart
Name: ta_openFile
Variables:
file$ - name of file to open
Output:
on success - the fileid of opened file
on failure - zero
Globals Used:
none
Description:
Check for a file and if it exists open it
RemEnd
function ta_openFile(file$)
// make sure the file exists
if GetFileExists(file$) = 0
oops_source = "Missing file '"+file$+"'"
exitfunction 0
endif
// attempt to open it
ifile = OpenToRead(file$)
// make sure it opened
if FileIsOpen(ifile) = 0
oops_source = "Failed to open '"+file$+"'"
exitfunction 0
endif
// good
endfunction ifile
RemStart
Name: ta_getNextLine
Variables:
iFile - file id of opened file
Output:
0 - end of file reached with nothing returned
1 - a non-comment, non-blank line has been found
Globals Used:
ta_aLine - set with line found in file or blank if none found
Description:
Read a file until either end of file or a non-blank
line not starting with '#' (a comment) is found.
If a line is found it is stored in the ta_aLine global
variable.
RemEnd
function ta_getNextLine(iFilID)
ta_aLine = ""
// make sure the file is actually open
if FileIsOpen(iFilID)
// try to find a non-blank, non-comment line
while fileEOF(iFilID) = 0
// grab a line
ta_aLine = readLine(iFilID)
// check for comment
if Left(ta_aLine,1) = "#"
// set to blank
ta_aLine = ""
else
// trim it
ta_aLine = ta_trimString(ta_aLine)
endif
// check for good, if not blank exit the function indication line found
if ta_aLine <> "" then exitfunction 1
endwhile
// check for done with file
if fileEOF(iFilID) = 1
// close the ifle
closefile(iFilID)
endif
endif
// if we reach here then there were no non-blank, non-comment lines
endfunction 0
TYPE delpnts
ifst AS integer
ilen AS integer
ENDTYPE
RemStart
Name: ta_getNextLineAndSplit
Variables:
iFile - file id of opened file
delim - the delimiter string
reqpec - the minimum number of required pieces, 0 if no minimum
Output:
0 - end of file reached with nothing returned
1 - a non-comment, non-blank line has been found
Globals Used:
ta_aSet - string array set with pieces in line found, untouched of no line found
ta_asCnt - number of pieces in line found in file or zero if none found
Description:
Read a file until either end of file or a non-blank
line not starting with '#' (a comment) is found.
Break up the line into pieces, seperating them
where the delimiter string occurs.
If a line is found, then the pieces are stored in the
ta_aSet global array and the number of pieces is
stored in the ta_asCnt global variable.
RemEnd
function ta_getNextLineAndSplit(iFilID AS integer, delim AS string,reqpec AS integer)
// assume nothing found
oops_source = ""
line$ = ""
ta_asCnt = 0
ta_asInd = 0
// make sure the file is actually open
if FileIsOpen(iFilID)
// try to find a non-blank, non-comment line
while fileEOF(iFilID) = 0
// grab a line
line$ = readLine(iFilID)
// check for comment
if Left(line$,1) = "#"
// set to blank
line$ = ""
else
// trim it
line$ = ta_trimString(line$)
endif
// check for good, if not blank break out of loop
if line$ <> "" then exit
endwhile
// check for done with file
if fileEOF(iFilID) = 1
// close the ifle
closefile(iFilID)
endif
// check for line being returned
if line$ <> ""
// time to extract bits
// find how many there are
dim delat[20] AS delpnts
iCnt as integer
i as integer
sLen as integer
dLen as integer
iCnt = 1
sLen = len(line$)
dLen = len(delim)
// assume the whole line to start
delat[0].ifst = 1
delat[0].ilen = sLen
// check for others
for i = 1 to sLen
if mid(line$, i, dLen) = delim
delat[iCnt].ifst = i + dLen
if iCnt > 0 then delat[iCnt-1].ilen = i - delat[iCnt-1].ifst
iCnt = iCnt + 1
endif
next i
// save last value
delat[iCnt-1].ilen = sLen - delat[iCnt-1].ifst + 1
// resize the return thingy
dim ta_aSet[iCnt] AS string
// break up the line
if iCnt > 1
// we know where the delimiters are
for i = 0 to iCnt-1
ta_aSet[i] = ta_trimString(mid(line$,delat[i].ifst,delat[i].ilen))
next i
else
// only one element
ta_aSet[0] = line$
endif
// save the number of things found
ta_asCnt = iCnt
// check for minimum requirement
if reqpec > 0
if iCnt < reqpec
oops_source = "Minimum fields is "+str(reqpec)+", but only found "+str(iCnt)+" in '"+line$+"'"
exitfunction 0
endif
endif
// got something, exit the function indicating that there are items in ta_aSet
exitfunction 1
endif
endif
// if we reach here then there were no non-blank, non-comment lines
endfunction 0
RemStart
Name: ta_resetPieces
Variables:
none
Output:
none
Globals Used:
ta_asInd - the index of the next piece to use
Description:
Reset ta_asInd to zero
RemEnd
function ta_resetPieces()
// reset to start
ta_asInd = 0
endfunction piece
RemStart
Name: ta_getNextPiece
Variables:
none
Output:
if ta_asInd < (ta_asCnt-1) - return the piece at ta_asInd
else return blank
Globals Used:
ta_aSet - string array set with pieces in line found, untouched of no line found
ta_asCnt - number of pieces in line found in file or zero if none found
ta_asInd - the index of the next piece to use
Description:
Pick the next piece from ta_aSet as indicated by ta_asInd, if there is one.
Increment the index to set up for next piece.
RemEnd
function ta_getNextPiece()
// initialise return
piece$ = ""
// check for more
if ta_asInd >= ta_asCnt then exitfunction piece$
// grab the piece
piece$ = ta_aSet[ta_asInd]
// increment the index
INC ta_asInd
endfunction piece$
RemStart
Name: ta_getNextPieceAsInt
Variables:
none
Output:
if ta_asInd < (ta_asCnt-1) - return the piece at ta_asInd converted to integer
else return blank
Globals Used:
ta_aSet - string array set with pieces in line found, untouched of no line found
ta_asCnt - number of pieces in line found in file or zero if none found
ta_asInd - the index of the next piece to use
Description:
Pick the next piece from ta_aSet as indicated by ta_asInd, if there is one.
Increment the index to set up for next piece.
RemEnd
function ta_getNextPieceAsInt()
// initialise return
piece$ = ta_getNextPiece()
// convert to integer
i_piece = val(piece$)
endfunction i_piece
RemStart
Name: ta_getNextPieceAsFloat
Variables:
none
Output:
if ta_asInd < (ta_asCnt-1) - return the piece at ta_asInd converted to float
else return blank
Globals Used:
ta_aSet - string array set with pieces in line found, untouched of no line found
ta_asCnt - number of pieces in line found in file or zero if none found
ta_asInd - the index of the next piece to use
Description:
Pick the next piece from ta_aSet as indicated by ta_asInd, if there is one.
Increment the index to set up for next piece.
RemEnd
function ta_getNextPieceAsFloat()
// initialise return
piece$ = ta_getNextPiece()
// convert to integer
piece# = FloatVal(piece$)
endfunction piece#
'ta_' stands for 'Triassic Associates, Inc. function/data'. Triassic Associates, Inc. is my company, and I am the only entity in it.
Cheers,
Ancient Lady