Posted: 21st Jun 2007 23:54
File writing, it's never really worked for me, it just leaves me with a blank text file.
Can anyone help ?
Code:
+ Code Snippet
FUNCTION WRITEKEY()
   OPEN TO WRITE 1,NAME$
   FOR T=1 to ARRAY COUNT (STATICSTRING())
      OTHERSTRING$=OTHERSTRING$+STATICSTRING(T)
   NEXT T
   WRITE STRING 1,OTHERSTRING$
   CLOSE FILE 1
ENDFUNCTION
Posted: 22nd Jun 2007 1:35
Tutorial 4 here:

http://forum.thegamecreators.com/?m=forum_view&t=99497&b=10

TDK_Man
Posted: 22nd Jun 2007 8:08
Well, one of the times it tried to work is biting you in your code right now. I suspect it was the time that you had only 1 string in your string array, because the file is blank, and your code is one off on the array access, should start at 0, as opposed to 1.

open file to write will fail if the file exists already. Here is what I did to make it work:

+ Code Snippet
global name$ as string
dim staticstring() as string

name$ = "output.txt"
array insert at bottom staticstring()
staticstring() = "This is the first string in the array."
array insert at bottom staticstring()
staticstring() = "This is the second string in the array."
array insert at bottom staticstring()
staticstring() = "This is the third string in the array."
writekey()
end

FUNCTION WRITEKEY()
    if file exist(name$)
        delete file name$
    endif
   OPEN TO WRITE 1,NAME$
   FOR T=0 to ARRAY COUNT (STATICSTRING())
      OTHERSTRING$=OTHERSTRING$+STATICSTRING(T)
   NEXT T
   WRITE STRING 1,OTHERSTRING$
   CLOSE FILE 1
ENDFUNCTION
Posted: 23rd Jun 2007 4:45
When open file to write is used with the file already created, is it a silent error or does nothing just hapen......
Thanks
When I do not use make file in my code, the file isn't even created......
Posted: 23rd Jun 2007 5:31
Oh man I remember having this problem all the time and I got so frustrated. Then I realized something important: Open To Write fails unless the file doesn't exist. So, just put Delete File name$ at the beginning of this part of your code, and it should work nicely.

...

And I just realized jinzai posted that already. Doh!
Posted: 23rd Jun 2007 8:08
np, Brain111, btw nice jams!

It is silent. That is standard for file creation in Windows, too, except that those functions return something to indicate the failure, and therefore...what to do about it. When you create a file in DOS, or Windows, there are two ways...both return an error code, and it is pretty easy to get the exact error. Plus, you can open files in many modes that make it easier to work with them.

In this case, the error is 'File already exists.'

One more thing about the file I/O you are using...you can't open a file for appending using that method. I think you would want to open it to read...read it all in, and update and rewrite the whole thing.

I use Windows for file I/O mostly. There is some setup involved, but it gives you access to a more complete file system. You can also use the c-runtime dll, but that stuff is not exactly easy to begin with, then again...what is?
Posted: 23rd Jun 2007 18:30
Hey thanks all, I think I see what i'm doing wrong
Posted: 24th Jun 2007 2:51
+ Code Snippet
REM Project: file in out
REM Created: 31/12/2006 22:18:33
REM
REM ***** Main Source File *****
REM
type HealthPack
 File as string
 x as float
 y as float
 z as float
 Rotate as byte
endtype

dim Item(100) as HealthPack

Item(1).File="HealthPack.x"
Item(1).x=223
Item(1).y=223
Item(1).z=0
Item(1).Rotate=1

Item(2).File="AmmoClip.x"
Item(2).x=127
Item(2).y=340
Item(2).z=20
Item(2).Rotate=1

Item(3).File="StopSign.x"
Item(3).x=427
Item(3).y=140
Item(3).z=0
Item(3).Rotate=0

` Check for the data file and delete it
`  In case you want to edit the array and re-run it
if file exist("TestData.dat")
 delete file "TestData.dat"
endif

` Save the data
open to write 1,"TestData.dat"
for t=1 to 3
 write string 1,Item(t).File
 write float 1,Item(t).x
 write float 1,Item(t).y
 write float 1,Item(t).z
 write byte 1,Item(t).Rotate
next t
close file 1

` Clear the data
for t=1 to 3
 Item(t).File=""
 Item(t).x=0
 Item(t).y=0
 Item(t).z=0
 Item(t).Rotate=0
next t

` Show the data (to show that the array is empty)
print "Data cleared"
print ""
for t=1 to 3
 print "Item("+str$(t)+")"
 print " Filename = "+Item(t).File
 print " x coordinate = "+str$(Item(t).x)
 print " y coordinate = "+str$(Item(t).y)
 print " z coordinate = "+str$(Item(t).z)
 print " Rotate = "+str$(Item(t).Rotate)
 print ""
 print ""
next t

wait key

` Read the data
open to read 1,"TestData.dat"
for t=1 to 3
 read string 1,Item(t).File
 read float 1,Item(t).x
 read float 1,Item(t).y
 read float 1,Item(t).z
 read byte 1,Item(t).Rotate
next t
close file 1

` Show the data (after its been loaded from the file)
cls
print "Data loaded from file."
print ""
for t=1 to 3
 print "Item("+str$(t)+")"
 print " Filename = "+Item(t).File
 print " x coordinate = "+str$(Item(t).x)
 print " y coordinate = "+str$(Item(t).y)
 print " z coordinate = "+str$(Item(t).z)
 print " Rotate = "+str$(Item(t).Rotate)
 print ""
 print ""
next t

wait key