please dont rip this site

Learning to Program with the Cybiko Handheld Computer Using B2C

Chapter 18 :  File I/O

Files on the Cybiko are stored in the non-volatile memory.  They are like files on the PC in that they have filenames and they hold data.  They even have attributes (like file size), but these attributes are not accessible from B2C.

Open a file

Before a file can be accessed, it must be opened.  In B2C, opening a file is accomplished with the Open command.  The Open command has three parts – the pathname, the mode, and the filenumber.  The mode is optional.

open pathname [for mode] as filenumber

The pathname is the name of the file.  This can be either a literal string ("filename.dat") or a variable which has been Dim'd previously.  The mode is optional and is one of Read, Write, or Append.  If left blank, the mode defaults to Read.  When a file is opened for Read access the program may only read data from the file, no writing is permitted.  Likewise, when a file is opened for Write, no data may be read from the file, only written.  If the file already exists, the data in the file is erased. When a file is opened for Append, it is opened for write, but if the file exists, instead of destroying the data, the file pointer is positioned at the end of the file and writing begins there.  Finally, the filenumber is a number from 0 to 7. It is used to identify the file for the rest of the program.

Dim fname[32] as char
print "Enter fname"
input fname
open fname for read as 1

It is possible to open the same file for Read in more than one place in the program.  But for Write and Append modes you must first close the file before reopening it.

Close a file

When you are done with a file, you must close it.  The Close command takes as its only parameter the filenumber…

open "filename.dat" as 1
 'do some stuff
close 1

Writing to a File : Put

The Put statement writes data from a variable to a file.  It has three parts: filenumber, bytepos, and variable

Put filenumber, [bytepos][, variable]

The Put command keeps the concept of the file position.  Each time data is written to the file the file position is incremented by the size of the variable in bytes.  In this way, you can accurately control the data being written to the file.  The first byte in the file is byte 0, the next is byte 1, etc…

dim foo as int
open "filename.dat" for write as 1
put 1, 0, foo  ' write two bytes at the beginning of the file
put 1, 100, foo ' write the same bytes at the 100th byte of the file

Leaving out the variable name positions the file pointer, but does not write

dim foo as int
open "filename.dat" for write as 1
put 1, 0  ' position file pointer to the beginning of the file

To write to the current file pointer position without specifying the value, leave out the bytepos (but remember to include the delimiting commas).  Unfortunately string variables and other arrays cannot be written with the Put command.  You must create a loop and write each element individually.

dim a as double
open "filename.dat for write as 1
input a
put 1,,a ' write 8 bytes to the current location

Reading from a file : Get

The Get statement reads data from a file into a variable.  It has three parts: filenumber, bytepos, and variable

Get filenumber, [bytepos][, variable]

The Get command keeps the concept of the file position.  Each time data is read from the file the file position is incremented by the size of the variable in bytes.  In this way, you can accurately control the data being read from the file.  The first byte in the file is byte 0, the next is byte 1, etc…

dim foo as int
open "filename.dat" for read as 1
get 1, 0, foo  ' read two bytes from the beginning of the file
get 1, 100, foo ' read two different bytes from the 100th byte of the file

Leaving out the variable name positions the file pointer, but does not read

dim foo as int
open "filename.dat" for read as 1
get 1, 0  ' position file pointer to the beginning of the file

To read from the current file pointer position without specifying the value, leave out the bytepos (but remember to include the delimiting commas).

dim a as double
open "filename.dat for read as 1
get 1,,a ' read 8 bytes from the current location
print a

Printing to the file : Print #

Printing to a file is possible with the print statement, which you are already familiar with.  Just add a "#n" where n is the filenumber.  This is an easy way to create text files.  Strangely, there is no corresponding Input # command for reading data.

dim a[10] as char
input "your name", a
open "filename.txt" for write as 1
print #1, a
close 1


file: /Techref/cybiko/b2c/ch18.htm, 5KB, , updated: 2008/6/13 16:18, local time: 2024/11/21 21:03,
TOP NEW HELP FIND: 
3.14.249.104:LOG IN

 ©2024 These pages are served without commercial sponsorship. (No popup ads, etc...).Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE. Questions?
Please DO link to this page! Digg it! / MAKE!

<A HREF="http://ecomorder.com/techref/cybiko/b2c/ch18.htm"> cybiko b2c ch18</A>

After you find an appropriate page, you are invited to your to this massmind site! (posts will be visible only to you before review) Just type a nice message (short messages are blocked as spam) in the box and press the Post button. (HTML welcomed, but not the <A tag: Instead, use the link box to link to another page. A tutorial is available Members can login to post directly, become page editors, and be credited for their posts.


Link? Put it here: 
if you want a response, please enter your email address: 
Attn spammers: All posts are reviewed before being made visible to anyone other than the poster.
Did you find what you needed?

 

Welcome to ecomorder.com!

 

Welcome to ecomorder.com!

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

  .