Programming C, C++, Java, PHP, Ruby, Turing, VB
Computer Science Canada 
Programming C, C++, Java, PHP, Ruby, Turing, VB  

Username:   Password: 
 RegisterRegister   
 Reading Files, Help Needed
Index -> Programming, Turing -> Turing Help
View previous topic Printable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic
Author Message
Guest




PostPosted: Sun May 28, 2006 11:39 am   Post subject: 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?
Sponsor
Sponsor
Sponsor
sponsor
Cervantes




PostPosted: Sun May 28, 2006 12:03 pm   Post subject: Re: Reading Files, Help Needed

vahnx wrote:

"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,
code:

File.Size(filename) = 0

would determine whether the file is empty or not.

If not, then try something like this:
code:

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




PostPosted: Sun May 28, 2006 12:08 pm   Post subject: (No subject)

Or, even better, take what Cervantes posted and turn it into a function, so you have your very own File.Size function Very Happy
Cervantes




PostPosted: Sun May 28, 2006 12:39 pm   Post subject: (No subject)

Exactly. Except that would be more along the lines of a File.Empty functon.

code:

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"?)
Guest




PostPosted: Sun May 28, 2006 12:48 pm   Post subject: (No subject)

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




PostPosted: Sun May 28, 2006 12:57 pm   Post subject: (No subject)

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?
Guest




PostPosted: Sun May 28, 2006 1:11 pm   Post subject: (No subject)

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.
Display posts from previous:   
   Index -> Programming, Turing -> Turing Help
View previous topic Tell A FriendPrintable versionDownload TopicSubscribe to this topicPrivate MessagesRefresh page View next topic

Page 1 of 1  [ 7 Posts ]
Jump to:   


Style:  
Search: