Difference between revisions of "File Operations"

From SoftMC-Wiki
Jump to: navigation, search
 
m (1 revision)
(No difference)

Revision as of 12:45, 23 May 2011

Input and output, whether to or from files, are supported on Flash and RAM devices. The files are accessed either in serial or random order. Currently, only ASCII text files are supported – both printable and non-printable characters.

OPEN

Opens an existing file or creates a new one with the name specified in the string expression. The file name should not exceed 8 characters plus extensions. Acceptable file extensions are:

PRG , DAT , TSK , CMP for permanent files on the Flash disk

REC for temporary files on the RAM disk

You can open a text file to read, write or append to the existing file according to mode flag.

“r” - open text file for reading

“w” - truncate to zero length or create text file for writing

“a” - append; open or create text file for writing at end-of-file

Use APPEND to add new lines at the end of the original contents of the file.Use WRITE to overwrite the previous file or to create a new file.

OPEN #, INPUT #, CLOSE, LOC

See Serial port.

TELL

Returns current file pointer, offset from the beginning of the file. Value has meaning only for SEEK to move the file pointer within a file.

SEEK

Moves file pointer to specified location. Use with TELL.

File operations example:

program

dim pos as long

dim d1 as double

dim s1 as string

'create file for writing

Open "test.prg" mode ="w" as #1

print #1, "1.2345"

close #1

'append to existing file

Open "test.prg" mode ="a" as #1

print #1, PI

close #1

'read from file

Open "test.prg" mode ="r" as #1

'how many characters in the file?

print "File contains ";loc(1);" characters"

s1 = input$(#1)

'convert from string to double

d1=val(s1)

?d1

'save current file pointer

pos = tell(#1)

?input$(#1)

'rewind file

seek (#1,pos)

?input$(#1)

close #1

end program


Output:

File contains 31 characters

1.234500000000000e+00

3.141592653590000e+00

3.141592653590000e+0