Re: Game Du Skill

new topic     » goto parent     » topic index » view thread      » older message » newer message

> Hey, all.
>
>
>     I'm thinking of starting a role-playing game and I thought I'd use the
>     Dragon Warrior top down view. I then got to thinking I should use a TXT file,
>     with a series of numbers that represent the
> landscape.
>
> Like this:
>
> 1=grass plain
> 2=hills
>
> 11112222111
>
> Which would make 4 squares of grass plains, the 4 hills, the 3 more grass
> plains. The problem is, I don't know how to read and write files. Even a text
> file for crying out loud. So, if one of you
> great big gurus in the sky could teach me how to read and write text files.
>
>
> -----Thanks
> -------------------/\====Thomas


Reading from a file is almost the same as reading from the keyboard,
you mostly use the same functions.
File can be opened in either BINARY or ASCII mode. In ASCII mode all
carriage-return/newline pairs will be converted to a '/n' (newline) character
and all '/n' characters being output will be convert to a
carriage-return/newline, this is the mode you should use for what your
doing.

OPENING THE FILE
You have to tell the operating system that you want to use the file and
what you want to use it for, you do this using the open() function:

     FileNumber = open(Filename, Mode)

Filename is the name of the file you want to open, for example
"test.txt"

Mode is a string containing one or two letters to indicate what you
want to do with the file. Use "r" if you want to read the contents of
the file, "w" if you want to output to a file (any existing file of
the same name will be deleted) or "a" if you want to start writing at
the end of an existing file (append data to it). You can append "b" to
the Mode string to open the file in BINARY mode but if you leave it
off then ASCII mode will be assumed.

FileNumber is a number returned by open() that identifies the file,
you must use this number wherever you read or write to the file.

READING FROM A TEXT FILE ONE LINE AT A TIME
You can read a file one line at a time using the gets() function. The
gets() function will return the next line in the file as a sequence or
a -1 if there are no more lines to read:

     Line = gets(FileNumber)

Line should be an object, after each call to gets check if Line is an
atom, if it is then then you have finished reading the file.
FileNumber should be a file number returned by open. Make sure you
open the file for reading (with mode set to "r").

WRITING TO A TEXT FILE
The easiest way to write to a file is to use the puts() function,
used exactly the same as when writing to the screen except use the
filenumber in place of 1:

     puts(FileNumber,String)

Make sure you open the file for writing or appending (with mode set to
"w" or "a").

CLOSING THE FILE
You should always close the file after using it, do this with the
close() function:

     close(FileNumber)

EXAMPLE

atom FileNumber
object Line

--Create a new text file for writing
FileNumber = open("test.txt","w")
--Write to the file
puts(FileNumber,"First line of test file!\nAnd another line!\n")
puts(FileNumber,"WOW! A third line!!!!!")
--And close it
close(FileNumber)

--Reopen the file for reading
FileNumber = open("test.txt","r")
while 1 do
        --Read in the next line
        Line = gets(FileNumber)
        --Check if we've reached the end of the file
        if atom(Line) then
                --We have, exit the loop
                exit
        end if
        --Print the line on the screen so that we can check that it's working
        puts(1,Line)
end while
--Close the file
close(FileNumber)

Hope that's helpful or at least makes some sort of sense, it's a
little late here (ok, not that late, but I'm very tired all the same:)

Thomas Parslow (PatRat) ICQ #:26359483
Rat Software
http://www.rat-software.com/
Please leave quoted text in place when replying

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu