Dark Bit Factory & Gravity

PROGRAMMING => Other languages => Blitz => Topic started by: asdflkj on June 05, 2006

Title: Text Files[BB2D]
Post by: asdflkj on June 05, 2006
-
Title: Re: Text Files
Post by: Clyde on June 05, 2006
In your code that reads in the data you'd usually do something along the lines of using a Dim VariableName(xamount,yamount) or however many slots, and not in the text file matey.
Title: Re: Text Files
Post by: Shockwave on June 06, 2006
I'll knock something up for you in a little while today mate.
Title: Re: Text Files
Post by: Shockwave on June 06, 2006
Ok, here's something very quick for you but it should be what you need;

Code: [Select]
;
; Simple file IO Example. By Shockwave^DBF
;
;------------------------------------------
Graphics 800,600,0,2

Dim STORE (4); space to read some data into
Dim FROMFILE (4); space to read a file into

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
 
;read the data from data statements;
For A=1 To 4
Read STORE(A)
Next

;CREATE A FILE
FILE_OUT = WriteFile("DATA.DAT")

;write the data into the file;
For A=1 To 4
WriteInt (FILE_OUT , STORE(A)); NB THIS COULD ALSO BE WRITEBYTE AND WRITESTRING TOO, ALL IN THE SAME FILE
Next

; ****ALWAYS CLOSE THE FILE WHEN YOU'VE FINISHED WITH IT!****
CloseFile(FILE_OUT)

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

;OPEN THE FILE AGAIN FOR READING;
FILE_IN = ReadFile ("DATA.DAT")

;Read the data from the file;

For A=1 To 4
FROMFILE(A)=ReadInt(FILE_IN) ; COULD ALSO BE READBYTE OR READSTRING
Next

; ****AGAIN MAKE SURE YOU CLOSE THE FILE WHEN YOU'VE FINISHED WITH IT****
CloseFile (FILE_IN)

;%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%


;OUTPUT THE RESULT;

Text 0,0,"ESCAPE TO EXIT, DATA READ FROM FILE IS BELOW;"
For A=1 To 4
Text 0,A*10,FROMFILE(A)
Next

While Not KeyDown(1)
Wend

Data 23,24,2000,100

Nb. You don't need to worry about the file pointer, it is managed by Blitz for you in a pretty similar way to data statements. If you think of it like that then you won't go far wrong. You do have more control over it than that if you want, but for your purposes this source should be enough information for now.
Title: Re: Text Files
Post by: asdflkj on June 06, 2006
-
Title: Re: Text Files
Post by: Shockwave on June 06, 2006
No worries, post back if you get stuck again.
Title: Re: Text Files
Post by: asdflkj on June 06, 2006
-
Title: Re: Text Files
Post by: Shockwave on June 07, 2006
You're very welcome Mark :) Glad I could help.
Title: Re: Text Files
Post by: asdflkj on June 08, 2006
-
Title: Re: Text Files
Post by: Shockwave on June 09, 2006
You need to use the seekfile command, basically you use it like this;

seekfile ( FILE_HANDLE , POSITION )

FILE_HANDLE is the variable of the file, eg;

FILE_HANDLE = openfile ("file.dat")

FILEPOS doesn't actually reset the file for you, it returns the position of the file pointer in bytes in the file.

If you want to reset the file pointer then you'll need to use Searchfile or something like that to find the location in the file that you want to reset to and then use Seekfile to reset the file pointer.

Hope that helps :)
Title: Re: Text Files
Post by: Shockwave on June 09, 2006
Nb, just realised that that's probably not too clear, so just to add;

Seekfile ( FILE_HANDLE , POSITION )

You know about the file handle, position is the number of bytes into the file to offset.

You were probably getting a MAV because you may have been outside the boundaries of the file.
Title: Re: Text Files
Post by: asdflkj on June 09, 2006
-
Title: Re: Text Files
Post by: Shockwave on June 09, 2006
Searchfile will look for something in the file so you could write a 1 byte number somewhere you want to reset to, something like $ff (as long as it ain't anywhere else in the file) as this will make it easier. then you can use searchfile to find that place in the file and then add 1 to the result and use seekfile to move to the location in the file. Clear as mud?
Give it a go and if it doesn't work I'll help you some more.
Title: Re: Text Files
Post by: asdflkj on June 09, 2006
-
Title: Re: Text Files
Post by: Shockwave on June 09, 2006
Well done :)