Difference between revisions of "Program Examples:File Handling:Open Read Write"
(Created page with "{{Axystems:Template:File_Handling_Example |DESCRIPTION= The following example demonstrates the set up of a TCP-IP server that opens connection with multiple clients.<br/> <br...") |
|||
(15 intermediate revisions by 3 users not shown) | |||
Line 1: | Line 1: | ||
− | {{ | + | {{Languages|Program_Examples:File_Handling:Open_Read_Write}} |
+ | 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. | ||
− | + | <syntaxhighlight lang="vb"> | |
− | + | program | |
− | |||
− | |||
− | |||
− | |||
− | |||
− | + | dim pos as long | |
− | + | dim d1 as double | |
− | + | dim s1 as string | |
− | 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 | ||
+ | </syntaxhighlight> | ||
+ | |||
+ | |||
+ | The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT. | ||
+ | |||
+ | |||
+ | == See Also == | ||
+ | * [[MC-Basic:OPEN|OPEN]] | ||
+ | * [[MC-Basic:LOC|LOC]] | ||
+ | * [[MC-Basic:PRINT|PRINT]] | ||
+ | * [[MC-Basic:INPUT$|INPUT$]] | ||
+ | * [[MC-Basic:TELL|TELL]] | ||
+ | * [[MC-Basic:SEEK|SEEK]] | ||
+ | * [[MC-Basic:CLOSE|CLOSE]] |
Latest revision as of 08:40, 16 July 2017
Language: | English • 中文(简体) |
---|
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
The example corresponds to commit SHA-1: ada143a7f402e6bbad24c13c56401a5393ce3d2b in GIT.