How to Make a file?

Post your coding questions such as 'how to' here.
Post Reply
coolw
Newcomer
Posts: 39
Joined: Wed Aug 29, 2007 11:17 pm
Location: West Virginia
Contact:

How to Make a file?

Post by coolw »

How do I make a file if one does not exist?

Code: Select all

ex = FileExists ("Data.txt")
so that if Data.txt does not exist, I can make one containing data?
My car game! (WIP)
viewtopic.php?f=18&t=53

Code: Select all

Coolw is the best! :)
User avatar
Sami The Great
Advanced Member
Posts: 485
Joined: Tue Aug 28, 2007 4:15 pm
Contact:

Re: How to Make a file?

Post by Sami The Great »

You can make new file by using OpenToEdit command. If file does not exist it will create a new one.
http://www.rockodilegames.com
CoolBasickin käyttäjä vuodesta 2004.
coolw
Newcomer
Posts: 39
Joined: Wed Aug 29, 2007 11:17 pm
Location: West Virginia
Contact:

Re: How to Make a file?

Post by coolw »

How do I add data into the file?
Lets say I wanted to add a name to the file, "Name$", do I,

WriteByte
WhiteShort
WriteInt
WriteFloat

What they do would be nice, for all of them, since I will probably be using most of them.
My car game! (WIP)
viewtopic.php?f=18&t=53

Code: Select all

Coolw is the best! :)
temu92
Web Developer
Web Developer
Posts: 1226
Joined: Mon Aug 27, 2007 9:56 pm
Location: Gamindustri
Contact:

Re: How to Make a file?

Post by temu92 »

If you want write a string into file use WriteString
User avatar
Jonez
Devoted Member
Posts: 575
Joined: Mon Aug 27, 2007 8:37 pm

Re: How to Make a file?

Post by Jonez »

If you define a variable as byte, you can store only 256 integers to it: digits 0 - 255. And as it's name says, a byte uses one byte from memory.

Short is equal to two bytes (two bytes from memory): 256 ^ 2 = 65536. So a variable defined as short can only be an integer between 0 - 65536.

Following the same pattern, an integer (all cb's variables are integers by default) is four bytes, and can store any integer between -2147483647 and 2147483647.

A float takes also 4 bytes from memory, and as you can figure out from it's name, it can hold a float (a decimal number) between 3.4E +/- 38, which is a 7-digits long decimal number.

a string holds text, and it's size varies. A String uses 4 bytes of memory, plus one byte per every character it stores.

This all applies in programming, wether you define a variable or write to a file. WriteByte writes one byte to a file, an integer between 0 - 255. To write strings, use either WriteLine or WriteString. WriteLine writes readable lines, and I recommend using it instead of WriteString.

Now you could ask, why use WriteByte when WriteInt (writes an integer) will do? Well it's all about the memory. You don't have unlimited amount of it so use it wisely.
-Vuoden 2008 aloittelijan ystävä -palkinnon voittaja-
Image <- protestipelikilpailun voittaja.
Space War
User avatar
Jare
Devoted Member
Posts: 877
Joined: Mon Aug 27, 2007 10:18 pm
Location: Pori
Contact:

Re: How to Make a file?

Post by Jare »

coolw wrote:How do I add data into the file?
Lets say I wanted to add a name to the file, "Name$", do I,

WriteByte
WhiteShort
WriteInt
WriteFloat

What they do would be nice, for all of them, since I will probably be using most of them.
Jonez already mentioned the details of data types, so I'll just show a complete example on how to write varibale Name$ to a file:

Code: Select all

'Open a file for writing and get an handle for it into variable named f
f = OpenToWrite("testfile.txt") 'If a file exists with this name, OpenToWrite will delete it. Use OpenToEdit if you wish just to add text to an existing file.

'Write Name$ to the file we opened.
WriteLine f, Name$ 'Using WriteLine will produce text which will look fine when the file is opened in Notepad. Otherwise you could use WriteString which could be used to make savegame files etc.

'Finish the work by closing the file
CloseFile f
[/code]
Post Reply