Trying to load file immediately closes the program

Started by Cosmo, August 13, 2018, 16:09:39

Previous topic - Next topic

Cosmo

I'm working on a project in BlitzPlus, and am having issues with attempting to load files into RAM.

The program immediately terminates upon beginning to load a file, but not upon saving it
I was wondering if this was a DEP issue, but saving used to crash before I fixed it.

(portion of function load_file_information()

Code: BASIC

If checksum2 <> checksum
debug_throw_error_and_crash_violently("v4save.bb -> function load_map_information() -> checksum2 = ReadLine(currentfile$): File is corrupt, header missing or corrupt, file cannot be loaded", 19)
EndIf

Cls
Delay 750 ; why not

While Not Eof(currentfile2$)

ReadLine(currentfile2$)
b.block = New block
b\blocktype = Int ReadLine(currentfile2$)
b\x = Int ReadLine(currentfile2$)
b\y = Int ReadLine(currentfile2$)
b\sizex = Int ReadLine(currentfile2$)
b\sizey = Int ReadLine(currentfile2$)
b\colourr = Int ReadLine(currentfile2$)
b\colourg = Int ReadLine(currentfile2$)
b\colourb = Int ReadLine(currentfile2$)
b\mtex = Int ReadLine(currentfile2$)

Color b\colourr, b\colourg, b\colourb
Rect b\x,b\y,b\sizex,b\sizey,1
Color ocolourr, ocolourg, ocolourb

Delay 10
Wend
Notify("load complete")
Return
End Function


Thanks for the help in advance,

Cosmo.


fairgood

Do you have debug enabled ? that should normally give you a hint what's causing the issue

Matty

there's no 'readfile' or 'openfile' command...hence the while loop jumps out immediately.

Cosmo

The openfile is earlier in the code.


Here's the whole thing:
Code: BASIC


Function load_map_information()
currentfile$ = RequestFile("Open game","ldfl",False)
If currentfile$ = 0
debug_throw_error_and_crash_violently("v4save.bb -> function load_map_information() -> currentfile$ = RequestFile('Save game', 'ldfl', False): No file chosen", 18)
EndIf

currentfile2$ = OpenFile(currentfile$)
SeekFile currentfile$, 0
checksum2 = ReadLine(currentfile$)

If checksum2 <> checksum
debug_throw_error_and_crash_violently("v4save.bb -> function load_map_information() -> checksum2 = ReadLine(currentfile$): File is corrupt, header missing or corrupt, file cannot be loaded", 19)
EndIf

Cls
Delay 750 ; why not

While Not Eof(currentfile2$)

ReadLine(currentfile2$)
b.block = New block
b\blocktype = Int ReadLine(currentfile2$)
b\x = Int ReadLine(currentfile2$)
b\y = Int ReadLine(currentfile2$)
b\sizex = Int ReadLine(currentfile2$)
b\sizey = Int ReadLine(currentfile2$)
b\colourr = Int ReadLine(currentfile2$)
b\colourg = Int ReadLine(currentfile2$)
b\colourb = Int ReadLine(currentfile2$)
b\mtex = Int ReadLine(currentfile2$)

Color b\colourr, b\colourg, b\colourb
Rect b\x,b\y,b\sizex,b\sizey,1
Color ocolourr, ocolourg, ocolourb

Delay 10
Wend
Notify("load complete")
Return
End Function

Matty

Name your variables better.

You should be getting a crash from that code as you read a line...first line from the wrong currentfile $ variable.

_PJ_

You seem to have confused handles with string variables.

SOMESTRINGVAR$=RequestFile(...)
SOMEINTHANDLE%=ReadFile(SOMESTRINGVAR$)
SOMESTRINGVAR$=ReadString(SOMEINTHANDLE%)
SOMEINTVAR%=ReadInt(SOMEINTHANDLE)


So in your code:

Code: BASIC

currentfile2% = OpenFile(currentfile$)
SeekFile currentfile2%, 0
checksum2$ = ReadLine(currentfile2)

If (checksum2$ <> checksum)
debug_throw_error_and_crash_violently("v4save.bb -> function load_map_information() -> checksum2 = ReadLine(currentfile2): File is corrupt, header missing or corrupt, file cannot be loaded", 19)
EndIf

Cls
Delay 750 ; why not

While Not Eof(currentfile2)

ReadLine(currentfile2)
b.block = New block
b\blocktype = Int ReadLine(currentfile2)




It also seems likely give your code sample, that you are not adequately considering the format for your file reading and I expect you encounter problems there too.

As stated, your example above requires the following:

LINE-LINETERMIANTRO (= Checsksum string)
Then for each iteration:
LINE (for no purpose)
INT Blocktype (as converted from a LINE)
X,Y,SizeX,SizeY,ColourR,G,B,MTex all as integers convreted from LINES

************************


I recommend storing data in the correct format you wish to read it. So use ReadInt() or ReadByte for the RGB values.
finally, please remember to CLOSE the filestream handle when you're done!