
-----------------------------------

Sun May 28, 2006 11:39 am

Reading Files, Help Needed
-----------------------------------
Whenever I have a game and a save file, if the file doesnt exists, it creates a new one then saves. If the file exists, it checks for the saved data, then overwrights it. But the problem is, that if I clear all the data from the file, but I dont delete the file, I get an error because the save procedure knows the file exists, but it can't use the "get:" because there is nothing to "get" from the file. 

So my question is:
"How can I make it so I can read the file and determine if nothing is in it without the error message?" 

NOTE: I can't remember if I fixed this error before, because we worked lots with files in school. Does it have to do with eof or something?

-----------------------------------
Cervantes
Sun May 28, 2006 12:03 pm

Re: Reading Files, Help Needed
-----------------------------------

"How can I make it so I can read the file and determine if nothing is in it without the error message?" 


Is there a File.Size function, or something of the sort, that returns the number of bytes the file is? If so,

File.Size(filename) = 0

would determine whether the file is empty or not.

If not, then try something like this:

var line : string
var lines := 0
% open file
loop
    exit when eof(file_stream)
    get : file_stream, line
    lines += 1
end loop
if lines = 0 then
    % file is empty
else
    % file is not empty
end if


-----------------------------------
TheOneTrueGod
Sun May 28, 2006 12:08 pm


-----------------------------------
Or, even better, take what Cervantes posted and turn it into a function, so you have your very own File.Size function :D

-----------------------------------
Cervantes
Sun May 28, 2006 12:39 pm


-----------------------------------
Exactly. Except that would be more along the lines of a File.Empty functon.


function FileSize(filename : string) : int
    var f_stream : int
    var line : string
    var bytes := 0
    open : f_stream, filename, get
    loop
        exit when eof (f_stream)
        get : f_stream, line
        bytes += length (line)
    end loop
    result bytes
end FileSize

You might have to add some to 'bytes' to account for newlines "\r\n" (or is it "\n\r"?)

-----------------------------------

Sun May 28, 2006 12:48 pm


-----------------------------------
I dont know what I did but I need help! I somehow messed up my game and I cant fix it. The one I posted in the Source section was working, and now somehow isnt??? I dont rreember removing anything. I need help badly

-----------------------------------
MysticVegeta
Sun May 28, 2006 12:57 pm


-----------------------------------
read the code on that section that you posted, it reads in from a file that does NOT exist, how do you expect it to work without outputting an error?

-----------------------------------

Sun May 28, 2006 1:11 pm


-----------------------------------
Ok i think im getting it to work now. I must of been half way on doing that, and saved it and put it on the forums thinking it was done, or something.
