Difference between revisions of "Program Examples:File Handling:Open Read Write"

From SoftMC-Wiki
Jump to: navigation, search
m
Line 1: Line 1:
{{Axystems:Template:File_Handling_Example
+
The following example demonstrates how to open a file with different permissions (Read Only, Write Only, Append), how to read from and write to the file, and how to manipulate the file pointer.
  
|DESCRIPTION=
+
<pre>
The following example demonstrates how to open a file with different permissions (Read Only, Write Only, Append),<br/>
+
program
how to read from and write to the file, and how to manipulate the file pointer.<br/>
+
dim pos as long
<br/>
+
dim d1 as double
 +
dim s1 as string
  
|EXAMPLE=
+
'create file for writing
program<br/>
+
Open "test.prg" mode = "w" as #1
<br/>
+
print #1, "1.2345"
dim pos as long<br/>
+
close #1
dim d1 as double<br/>
 
dim s1 as string<br/>
 
<br/>
 
'create file for writing<br/>
 
Open "test.prg" mode = "w" as #1<br/>
 
print #1, "1.2345"<br/>
 
close #1<br/>
 
<br/>
 
'append to existing file<br/>
 
Open "test.prg" mode = "a" as #1<br/>
 
print #1, PI<br/>
 
close #1<br/>
 
'read from file<br/>
 
Open "test.prg" mode = "r" as #1<br/>
 
<br/>
 
'how many characters in the file?<br/>
 
print "File contains ";loc(1);" characters"<br/>
 
s1 = input$(#1)<br/>
 
<br/>
 
'convert from string to double<br/>
 
d1=val(s1)<br/>
 
?d1<br/>
 
<br/>
 
'save current file pointer<br/>
 
pos = tell(#1)<br/>
 
print "File pointer Position = ";pos<br/>
 
?input$(#1)<br/>
 
<br/>
 
'rewind file<br/>
 
seek (#1,pos)<br/>
 
?input$(#1)<br/>
 
close #1<br/>
 
<br/>
 
end program<br/>
 
  
|SEE ALSO=
+
'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)
 +
print "File pointer Position = ";pos
 +
?input$(#1)
 +
 
 +
'rewind file
 +
seek (#1,pos)
 +
?input$(#1)
 +
close #1
 +
 
 +
end program
 +
</pre>
 +
 
 +
== See Also ==
 
* [[Axystems:MC-Basic:OPEN|OPEN]]
 
* [[Axystems:MC-Basic:OPEN|OPEN]]
 
* [[Axystems:MC-Basic:LOC|LOC]]
 
* [[Axystems:MC-Basic:LOC|LOC]]
Line 53: Line 48:
 
* [[Axystems:MC-Basic:SEEK|SEEK]]
 
* [[Axystems:MC-Basic:SEEK|SEEK]]
 
* [[Axystems:MC-Basic:CLOSE|CLOSE]]
 
* [[Axystems:MC-Basic:CLOSE|CLOSE]]
 
}}
 

Revision as of 14:06, 2 April 2014

The following example demonstrates how to open a file with different permissions (Read Only, Write Only, Append), how to read from and write to the file, and how to manipulate the file pointer.

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)
	print "File pointer Position = ";pos
	?input$(#1)

	'rewind file
	seek (#1,pos)
	?input$(#1)
	close #1

end program

See Also