Error accessing a file after WriteFile

Blitz3D Forums/Blitz3D Beginners Area/Error accessing a file after WriteFile

3DFish(Posted 2004) [#1]
I have two functions.
One Writing game save data.
One Reading game save data.

Writing Function:
I use WriteFile to write the data to a file "savegame.dat"
Then I do a CloseFile

Reading Function:
I used Openfile or ReadFile to Open the file "savegame.dat"

My problem is that the file have a size of 0 kb
Although the data was written to it.

When I terminate the app, all the data shows in the file.
The file size is then 700 kb.

For some reason I cannot seem to access the file immediately after writing any data to it

Any one heard of this before?


Ricky Smith(Posted 2004) [#2]
Are you putting in a CloseFile() after writing ?

EDIT - sorry -didn't notice you do CloseFile()
Maybe put in a small delay before re-opening ? Allow the system time to save any cached info.


eBusiness(Posted 2004) [#3]
Maybe you should show us the code.


soja(Posted 2004) [#4]
My guess:

You're using the file handle in the functions, the handle is declared outside of the functions, and is not declared global.


3DFish(Posted 2004) [#5]
I declared the file handle globally, but still have the same problem

Here is a piece of my code:

Write Function:

filewrite = WriteFile("files\savegame.dat")
If filewrite <> 0 Then ;file exists
;Player info
WriteLine(filewrite,"Player data")
For p.Player = Each Player
WriteLine(filewrite,"LOCX:"+EntityX(p\entity))
WriteLine(filewrite,"LOCY:"+EntityY(p\entity))
Next
EndIf

CloseFile(filewrite)

Read Function:

fileread = OpenFile("files\savegame.dat")

If fileread <> 0 Then ;file exists
While Not Eof(fileread)
fr1$=ReadLine( fileread )

line_int%=Instr(fr1,"LOCX:",1)
If line_int <> 0 Then
strcount=5
s$=Mid(fr1,line_Int+strcount,10) : pfx=s
EndIf

line_int%=Instr(fr1,"LOCY:",1)
If line_int <> 0 Then
strcount=5
s$=Mid(fr1,line_Int+strcount,10) : pfy=s
EndIf

line_int%=Instr(fr1,"LOCZ:",1)
If line_int <> 0 Then
strcount=5
s$=Mid(fr1,line_Int+strcount,10) : pfz=s
EndIf
Wend
EndIf

CloseFile(fileread)

Thanks for your replies..


eBusiness(Posted 2004) [#6]
Hmmm, both closefiles should be within the "if file opened"'s, but that shouldn't cause mentioned bug. Try replacing openfile with readfile. I'm still not shure what the bug might be.

Edit: I tried to replicate your bug, the code you have given seemed to work fine.


Rimmsy(Posted 2004) [#7]
use readfile instead of openfile


3DFish(Posted 2004) [#8]
I used BlitzDebugger and stepped into the code.

In the Read function:
I see that fileread <> 0 (it exists)

But when I do a ..While not Eof(fileread)..the pointer jumps to the end of the while loop, to CloseFile(fileread)

For some reason, the compiler thinks there are no data in the file..

very strange


3DFish(Posted 2004) [#9]
Yes!..Got it working

I did a CloseFile(writefile) again, after calling the Write function

Now it's working perfectly

Thanks for your replies guys