createfile? Any commands like this for creating a file?

Previous topic - Next topic

Amon

Is there a way to create a file from within code? Other languages have somethng like createfile etc.

I need them for my map editor.

Ta!
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

MrTAToad

Try OPENFILE - can open a file for reading or writing...

Amon

I have this:

Code (glbasic) Select
file$ = OPENFILE(1,"map1.txt",FALSE)
FOR xi2 = 0 TO mapwidth - 1
FOR yi2 = 0 TO mapheight - 1
WRITEIEEE 1,map[xi2][yi2]
NEXT
NEXT


It dunneh work!

Can you help?
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Ian Price

OPENFILE is indeed what is required, and it does work -

Code (glbasic) Select


GLOBAL map[100][100]
//
GLOBAL lvl // level number
//
// Save data
FUNCTION save_data:
//
LOCAL file$="Media/data/"+lvl+".dat"
//
LOCAL n%
//
OPENFILE (1,file$,FALSE)
//
FOR y=0 TO 22
FOR x=0 TO 22
  WRITEBYTE 1,map[x][y]
NEXT
NEXT
//
CLOSEFILE 1
//
ENDFUNCTION
//
//
// Load data
FUNCTION load_data:
//
LOCAL file$="Media/data/"+lvl+".dat"
//
LOCAL n%, ok
//
ok=DOESFILEEXIST(file$)
//
IF ok
//
OPENFILE (1,file$,TRUE)
//
FOR y=0 TO 22
FOR x=0 TO 22
  READBYTE 1,n
  map[x][y]=n
NEXT
NEXT
//
CLOSEFILE 1
//
ENDIF
//
ENDFUNCTION


This code also includes error trapping to ensure the file exists or not (don't load a file that isn't there!)

[EDIT] Argghhhhh the spacing is all fecked on this forum. I've used // to space it out a bit - it looks quite long as a result, but it isn't really.
I came. I saw. I played.

Moru

I don't know how GLBasic handles it but many other languages don't create a file unless you actually close the file when you are done writing.

Schranz0r

GLBasic create a file, and then write into it!
I <3 DGArray's :D

PC:
AMD Ryzen 7 3800X 16@4.5GHz, 16GB Corsair Vengeance LPX DDR4-3200 RAM, ASUS Dual GeForce RTX™ 3060 OC Edition 12GB GDDR6, Windows 11 Pro 64Bit, MSi Tomahawk B350 Mainboard

Amon

Thanks guys but I'm still getting a problem. When I remove this code from my game it works fine but when I uncomment it the game shuts down whenever I press F1 to access the savemap() function.

Can anyone spot what's going on?

Code (glbasic) Select

INC level,1
LOCAL file$ = "Media/Data/"+level+".map"
OPENFILE(1,file$,FALSE)
FOR xi2 = 0 TO mapwidth - 1
FOR yi2 = 0 TO mapheight - 1
WRITEBYTE 1,map[xi2][yi2]
NEXT
NEXT
CLOSEFILE 1
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Ian Price

Are you sure that your map[][] array isn't going out of bounds (using numbers larger than the max array size)? I presume you have actually created the array already.

Try using a fixed low number in the FOR/NEXT loop

ie instead of

FOR xi2 = 0 TO mapwidth - 1

use

FOR xi2 = 0 TO 5

and likewise for the yi2.

If you still get an error then it's likely not an array problems (unless your maps are smaller than 6x6!), but I'm presuming that it is an OOB. Usually the obvious reason for unexpected exit.
I came. I saw. I played.

Amon

Hi Ian, Just tried what you suggested and I still get the shutdown. Yes the map array is already created and used in the game without problems.

Stupidly I didnt think to turn the debugger on and this is what it spits out aftr the shutdown.

Code (glbasic) Select

Start debug session.
_______________________________________
Injection started

Error:
No file

- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Ian Price

That helps a bit. Usually it's not a case of the file not existing, but the directory. Ensure you have a "Data" directory where it should be. If not, create one and it should be ok. :)
I came. I saw. I played.

Amon

Hi Ian, yep the directorey is there. I always double check these things. The data directory is in the Media folder (case wise spelt properly) also.

Might this be a Win7 issue? I'm running windows 7 home premium.
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Ian Price

I'm running Windows7 Home Premium too.

My example definitely works, as I've used it in lots of projects (including my current one).

Are you sure that you've created GLOBAL variables for mapwidth/height and that they are spelled correctly? Also remember that GLBasic is case sensitive. If you've mis-spelled (or altered case) either then the array will be a negative integer.

[EDIT]
I've just tested your code in a new file and it works fine.

Code (glbasic) Select

DIM map[100][100]

GLOBAL mapwidth=10
GLOBAL mapheight=10
GLOBAL level

INC level,1

      LOCAL file$ = "Media/Data/"+level+".map"
      OPENFILE(1,file$,FALSE)
      FOR xi2 = 0 TO mapwidth - 1
         FOR yi2 = 0 TO mapheight - 1
            WRITEBYTE 1,map[xi2][yi2]
         NEXT
      NEXT
      CLOSEFILE 1
     
      PRINT "DONE",10,10
     
      SHOWSCREEN
     
      KEYWAIT
     
I came. I saw. I played.

Amon

hmmm! The directories exist. When I just try and create the file out of the media folder it works perfectly. I get my saved map and can load it back in. When it's set to the data folder it's a no go.  :S

Folder structure is like this:

game.app/Media/Data << That's the folder I try to save to but it just quits out saying no file.

if I save though to game.app it all works fine.

By the way I'm running the 64bit version of Win7. Surely that can't be the issue?
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!

Ian Price

Yep 64bit here too.

Try removing the "game.app/" bit - just save to "Media/Data/"

Did you try my/your code (above post) as a brand new project?

If you want to send your complete .app to me (or attach it here), I can try and work out what's going on  - iprice AT supanet DOT com
I came. I saw. I played.

Amon

Hi Ian, just sent my project folder with imges to you. Thanks for your help. :)
- http://www.amon.co | Dev Tools - BlitzMax - Shiva Unlimited - Unity Pro - Carrara 8 Pro - Hexagon 2.2.5 - Blacksmith 3D Paint - FuturePaint Pro - Bryce 7 Pro.
I'm never wrong at all; I just discover 1000 ways that don't work!