DIV ARENA FORUMS   » DIV GAMES STUDIO  » Hardcore Coders  » reading and writing files 
Select A Forum 
The Forum 
   » FORUM RULES 
   » Competitions 
    » Code Golf 
   » News 
   » User News 
   » DIV's future 
DIV GAMES STUDIO 
   » General Discussion 
    » DIV  
   » Beginners 
   » Hardcore Coders 
   » Technical Q&A 
   » Remakes 
   » Code-along with Mike 
    » Tutor 0 - Space shooter! 
    » Tutor 1 - Platfom Game 
   » WIPS 
   » Code Snippets 
    » DLLs 
   » Finished Games 
   » Resource Sharing 
   » DIV 1 + 2 (DOS) 
   » DIV 2 (Modern) 
   » DIV 3 (DX) 
    » DIV Windows 
    » DIV Linux 
    » DIV OSX 
    » DIV Android 
    » DIV 3DS 
    » DIV Raspberry Pi 
    » DIV HTML/JS 
    » DIV iPhone / iPad (IOS) 
    » DIV GCW Zero 
    » DIV Pandora 
    » DIV PSP 
   » Screenshots 
   » Time Out 
  
 
Casper 
DIV Pro
Posts: 31
Registered: 18-10-2017
Member Is Offline
 
 
 
reading and writing files 
 
Context: I'm writing a level editor for my game, I need to read and write some files.  Code: 
function SaveObject(string fileName, a)
private
    fileHandle;
begin
    debug;
    // open file handle
    fileHandle = fopen(DATA_OBJECTS_PATH + fileName, "w");
    // write object data
    fwrite(offset a, sizeof(a), fileHandle);
    // close file handle
    fclose(fileHandle);
end
Code: 
function LoadObject(string fileName)
private
    fileHandle;
    a;
begin
    debug;
    // open file handle
    fileHandle = fopen(DATA_OBJECTS_PATH + fileName, "r");
    // read object data
    fread(offset a, sizeof(a), fileHandle);
    // close file handle
    fclose(fileHandle);
end
Quote: 
 
 
 
 
CicTec 
DIV Pro
Posts: 471
Registered: 6-8-2016
Member Is Offline
 
 
 
 
Hi Casper,  Code: 
program test_fopen;
const
  DATA_OBJECTS_PATH = "";
global
  a;
begin
  SaveObject("__test_fopen.dat", 273);
  LoadObject("__test_fopen.dat");
  write_int(0, 160, 100, 4, &a);
  repeat
    frame;
  until(key(_esc));
end
function SaveObject(string fileName, a)
private
    fileHandle;
begin
    // open file handle
    fileHandle = fopen(DATA_OBJECTS_PATH + fileName, "w");
    // write object data
    fwrite(offset a, sizeof(a), fileHandle);
    // close file handle
    fclose(fileHandle);
end
function LoadObject(string fileName)
private
    fileHandle;
begin
    // open file handle
    fileHandle = fopen(DATA_OBJECTS_PATH + fileName, "r");
    // read object data
    fread(offset a, sizeof(a), fileHandle);
    // close file handle
    fclose(fileHandle);
end
 
 
 
 
Casper 
DIV Pro
Posts: 31
Registered: 18-10-2017
Member Is Offline
 
 
 
 
Hey CicTec, thanks for writing that! I should have done it myself, as it's more useful to test with. I tested with this:  Code: 
program test_fopen;
const
  DATA_OBJECTS_PATH = "assets/";
global
  a;
  b;
begin
  debug;
  SaveObject("__test_fopen.dat", 273);
  LoadObject("__test_fopen.dat");
  write_int(0, 160, 100, 4, &b);
  repeat
    frame;
  until(key(_esc));
end
function SaveObject(string fileName, a)
private
    fileHandle;
begin
    // open file handle
    fileHandle = fopen(DATA_OBJECTS_PATH + fileName, "w");
    // write object data
    fwrite(offset a, sizeof(a), fileHandle);
    // close file handle
    fclose(fileHandle);
end
function LoadObject(string fileName)
private
    fileHandle;
begin
    // open file handle
    fileHandle = fopen(DATA_OBJECTS_PATH + fileName, "r");
    // read object data
    fread(offset b, sizeof(b), fileHandle);
    // close file handle
    fclose(fileHandle);
end
 
 
 
 
Casper 
DIV Pro
Posts: 31
Registered: 18-10-2017
Member Is Offline
 
 
 
 
edit: Nevermind. I'm going crazy. There are probably some unintuitive things going on with the DIV compile/run loop that are giving my debugging
skills a run for their money. 
 
 
 
 
CicTec 
DIV Pro
Posts: 471
Registered: 6-8-2016
Member Is Offline
 
 
 
 
Hi Casper,  Quote: Originally posted by Casper    edit: Nevermind. I'm going crazy. There are probably some unintuitive things going on with the DIV compile/run loop that are giving my debugging
skills a run for their money. 
 
 
 
 
Casper 
DIV Pro
Posts: 31
Registered: 18-10-2017
Member Is Offline
 
 
 
 
Well, I stopped opening/viewing the .dat file while stepping through with the debugger. After that, it appeared that the issue (crazy large integers
in debugger, and corrupted files) went away. Now in order to validate my assumptions I wish to force the same error to occur again... Unfortunately, I
can't reproduce it even though I'm doing the same things: 1) step through and observe the values while the file handle is open, and 2) open/view the
file while the file handle is open. So now I'm doubting my own conclusions. I don't understand what caused the error, so I can't prevent myself from
making it again. 
 
 
 
 
CicTec 
DIV Pro
Posts: 471
Registered: 6-8-2016
Member Is Offline
 
 
 
 
Yes, you must keep in mind that opening a file in read only should not cause problems, the problem is that some editors automatically save the file
open after a certain period of time, and if you have made changes without saving you then find yourself with the changes file and possible data
corruption, if this is simultaneously open in another program for reading / writing, so not to go crazy, open the file with only one editor at a time.
 
 
 
 
 
 
 
 DIV ARENA FORUMS  » DIV GAMES STUDIO  » Hardcore Coders  » reading and writing files 
Select A Forum 
The Forum 
   » FORUM RULES 
   » Competitions 
    » Code Golf 
   » News 
   » User News 
   » DIV's future 
DIV GAMES STUDIO 
   » General Discussion 
    » DIV  
   » Beginners 
   » Hardcore Coders 
   » Technical Q&A 
   » Remakes 
   » Code-along with Mike 
    » Tutor 0 - Space shooter! 
    » Tutor 1 - Platfom Game 
   » WIPS 
   » Code Snippets 
    » DLLs 
   » Finished Games 
   » Resource Sharing 
   » DIV 1 + 2 (DOS) 
   » DIV 2 (Modern) 
   » DIV 3 (DX) 
    » DIV Windows 
    » DIV Linux 
    » DIV OSX 
    » DIV Android 
    » DIV 3DS 
    » DIV Raspberry Pi 
    » DIV HTML/JS 
    » DIV iPhone / iPad (IOS) 
    » DIV GCW Zero 
    » DIV Pandora 
    » DIV PSP 
   » Screenshots 
   » Time Out