Jump to content

File input/output: Difference between revisions

Dialects of BASIC moved to the BASIC section.
(Dialects of BASIC moved to the BASIC section.)
Line 847:
close 1
close 2</syntaxhighlight>
 
==={{header|BBC BASIC}}===
[[BBC BASIC for Windows]] has a file copy command:
<syntaxhighlight lang="bbcbasic">*COPY input.txt output.txt</syntaxhighlight>
Alternatively the copy can be done explicitly:
<syntaxhighlight lang="bbcbasic">infile% = OPENIN("input.txt")
outfile% = OPENOUT("output.txt")
WHILE NOT EOF#infile%
BPUT #outfile%, BGET#infile%
ENDWHILE
CLOSE #infile%
CLOSE #outfile%</syntaxhighlight>
 
==={{header|Commodore BASIC}}===
Line 862 ⟶ 874:
120 close 8
130 end</syntaxhighlight>
 
==={{header|FreeBASIC}}===
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
/'
input.txt contains:
 
The quick brown fox jumps over the lazy dog.
Empty vessels make most noise.
Too many chefs spoil the broth.
A rolling stone gathers no moss.
'/
 
Open "output.txt" For Output As #1
Open "input.txt" For Input As #2
Dim line_ As String ' note that line is a keyword
 
While Not Eof(2)
Line Input #2, line_
Print #1, line_
Wend
 
Close #2
Close #1</syntaxhighlight>
{{out}}
<pre>
output.txt contains:
 
The quick brown fox jumps over the lazy dog.
Empty vessels make most noise.
Too many chefs spoil the broth.
A rolling stone gathers no moss.
</pre>
 
==={{header|FutureBasic}}===
<syntaxhighlight lang="futurebasic">
 
/*
 
Rosetta Code File input/output example
FutureBasic 7.0.14
 
Rich Love
9/25/22
 
Before running this, use TextEdit to create a file called input.txt on your desktop.
Format as plain text and create a few lines of text.
Then save.
 
*/
 
output file "FileInputOutput.app"
 
CFURLRef ParentDirectory // Create a url for the desktop
ParentDirectory = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
 
CFURLRef inputURL // Create a url for input.txt on the desktop
inputURL = fn URLByAppendingPathComponent( ParentDirectory, @"input.txt" )
 
CFURLRef outputURL // Create a url for output.txt on the desktop
outputURL = fn URLByAppendingPathComponent( ParentDirectory, @"output.txt" )
 
open "O", 1, outputURL
open "I", 2, inputURL
 
str255 dataLine
 
While Not Eof(2)
Line Input #2, dataLine
Print #1, dataLine
Wend
 
Close #1
Close #2
 
end
</syntaxhighlight>
 
==={{header|Gambas}}===
<syntaxhighlight lang="gambas">Public Sub Main()
Dim sOutput As String = "Hello "
Dim sInput As String = File.Load(User.Home &/ "input.txt") 'Has the word 'World!' stored
 
File.Save(User.Home &/ "output.txt", sOutput)
File.Save(User.Home &/ "input.txt", sOutput & sInput)
 
Print "'input.txt' contains - " & sOutput & sInput
Print "'output.txt' contains - " & sOutput
 
End</syntaxhighlight>
Output:
<pre>
'input.txt' contains - Hello World!
'output.txt' contains - Hello
</pre>
 
==={{header|IS-BASIC}}===
Line 878 ⟶ 985:
220 CLOSE #2
230 END HANDLER</syntaxhighlight>
 
==={{header|Liberty BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="lb">nomainwin
 
open "input.txt" for input as #f1
qtyBytes = lof( #f1)
source$ = input$( #f1, qtyBytes)
close #f1
 
open "output.txt" for output as #f2
#f2 source$;
close #f2
 
end</syntaxhighlight>
 
==={{header|PureBasic}}===
====Basic file copy====
<syntaxhighlight lang="purebasic">CopyFile("input.txt","output.txt")</syntaxhighlight>
 
====Line by line====
<syntaxhighlight lang="purebasic">in = ReadFile(#PB_Any,"input.txt")
If in
out = CreateFile(#PB_Any,"output.txt")
If out
Define MyLine$
While Not Eof(in)
MyLine$ = ReadString(in)
WriteString(out,MyLine$)
Wend
CloseFile(out)
EndIf
CloseFile(in)
EndIf</syntaxhighlight>
 
====Reading & writing the complete file in one pass====
<syntaxhighlight lang="purebasic">If ReadFile(0,"input.txt")
Define MyLine$, *Buffer, length
length=FileSize("input.txt")
*Buffer = AllocateMemory(length)
If *Buffer
If OpenFile(1,"output.txt")
ReadData(0, *Buffer, length)
WriteData(1, *Buffer, length)
CloseFile(1)
EndIf
FreeMemory(*Buffer)
EndIf
CloseFile(0)
EndIf</syntaxhighlight>
 
==={{header|QBasic}}===
Line 889 ⟶ 1,046:
CLOSE #2
END</syntaxhighlight>
 
==={{header|RapidQ}}===
File I/O is one of the things where RapidQ differs from standard Basic. RapidQ uses file streams.
 
The first version copies text line by line, as in the ''BASIC'' example.
 
<syntaxhighlight lang="rapidq">$INCLUDE "rapidq.inc"
 
DIM File1 AS QFileStream
DIM File2 AS QFileStream
 
File1.Open("input.txt", fmOpenRead)
File2.Open("output.txt", fmCreate)
 
WHILE NOT File1.EOF
data$ = File1.ReadLine
File2.WriteLine(data$)
WEND
 
File1.Close
File2.Close</syntaxhighlight>
 
When just copying data, the code can be simplified by using the CopyFrom method.<br />
(The second parameter for CopyFrom is number of bytes to copy, 0 = copy the whole file.)
 
<syntaxhighlight lang="rapidq">$INCLUDE "rapidq.inc"
 
DIM File1 AS QFileStream
DIM File2 AS QFileStream
 
File1.Open("input.txt", fmOpenRead)
File2.Open("output.txt", fmCreate)
 
File2.CopyFrom(File1, 0)
 
File1.Close
File2.Close</syntaxhighlight>
 
==={{header|REALbasic}}===
<syntaxhighlight lang="vb">
Sub WriteToFile(input As FolderItem, output As FolderItem)
Dim tis As TextInputStream
Dim tos As TextOutputStream
tis = tis.Open(input)
tos = tos.Create(output)
While Not tis.EOF
tos.WriteLine(tis.ReadLine)
Wend
tis.Close
tos.Close
End Sub
</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
fileLen = LOF(#in) 'Length Of File
fileData$ = input$(#in, fileLen) 'read entire file
close #in
 
open "output.txt" for output as #out
print #out, fileData$ 'write entire fie
close #out
end
</syntaxhighlight>
 
Or directly with no intermediate fileData$
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
open "output.txt" for output as #out
fileLen = LOF(#in) 'Length Of File
print #out, input$(#in, fileLen) 'entire file
close #in
close #out
</syntaxhighlight>
 
==={{header|True BASIC}}===
Line 901 ⟶ 1,133:
CLOSE #2
END</syntaxhighlight>
 
==={{header|VBA}}===
<syntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Dim s As String, FF As Integer
 
'read a file line by line
FF = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\input.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, s
Debug.Print s
Wend
Close #FF
 
'read a file
FF = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\input.txt" For Input As #FF
s = Input(LOF(1), #FF)
Close #FF
Debug.Print s
 
'write a file
FF = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\output.txt" For Output As #FF
Print #FF, s
Close #FF
End Sub</syntaxhighlight>
 
==={{header|VBScript}}===
One liner (-2 for system default encoding)
<syntaxhighlight lang="vb">CreateObject("Scripting.FileSystemObject").OpenTextFile("output.txt",2,-2).Write CreateObject("Scripting.FileSystemObject").OpenTextFile("input.txt", 1, -2).ReadAll</syntaxhighlight>
 
==={{header|Visual Basic .NET}}===
{{works with|Visual Basic .NET|9.0+}}
====Byte copy====
<syntaxhighlight lang="vbnet">My.Computer.FileSystem.WriteAllBytes("output.txt", _
My.Computer.FileSystem.ReadAllBytes("input.txt"), False)
</syntaxhighlight>
 
====Text copy====
<syntaxhighlight lang="vbnet">Using input = IO.File.OpenText("input.txt"), _
output As New IO.StreamWriter(IO.File.OpenWrite("output.txt"))
output.Write(input.ReadToEnd)
End Using</syntaxhighlight>
====Line by line text copy====
<syntaxhighlight lang="vbnet">Using input = IO.File.OpenText("input.txt"), _
output As New IO.StreamWriter(IO.File.OpenWrite("output.txt"))
Do Until input.EndOfStream
output.WriteLine(input.ReadLine)
Loop
End Using</syntaxhighlight>
 
==={{header|Yabasic}}===
Line 920 ⟶ 1,206:
 
There may be other techniques too.
 
=={{header|BBC BASIC}}==
[[BBC BASIC for Windows]] has a file copy command:
<syntaxhighlight lang="bbcbasic">*COPY input.txt output.txt</syntaxhighlight>
Alternatively the copy can be done explicitly:
<syntaxhighlight lang="bbcbasic">infile% = OPENIN("input.txt")
outfile% = OPENOUT("output.txt")
WHILE NOT EOF#infile%
BPUT #outfile%, BGET#infile%
ENDWHILE
CLOSE #infile%
CLOSE #outfile%</syntaxhighlight>
 
=={{header|BCPL}}==
Line 1,896 ⟶ 2,170:
 
end program FileIO</syntaxhighlight>
 
=={{header|FreeBASIC}}==
<syntaxhighlight lang="freebasic">' FB 1.05.0 Win64
 
/'
input.txt contains:
 
The quick brown fox jumps over the lazy dog.
Empty vessels make most noise.
Too many chefs spoil the broth.
A rolling stone gathers no moss.
'/
 
Open "output.txt" For Output As #1
Open "input.txt" For Input As #2
Dim line_ As String ' note that line is a keyword
 
While Not Eof(2)
Line Input #2, line_
Print #1, line_
Wend
 
Close #2
Close #1</syntaxhighlight>
 
{{out}}
<pre>
output.txt contains:
 
The quick brown fox jumps over the lazy dog.
Empty vessels make most noise.
Too many chefs spoil the broth.
A rolling stone gathers no moss.
</pre>
 
=={{header|Frink}}==
Line 1,938 ⟶ 2,178:
w.close[]
</syntaxhighlight>
 
=={{header|FutureBasic}}==
<syntaxhighlight lang="futurebasic">
 
/*
 
Rosetta Code File input/output example
FutureBasic 7.0.14
 
Rich Love
9/25/22
 
Before running this, use TextEdit to create a file called input.txt on your desktop.
Format as plain text and create a few lines of text.
Then save.
 
*/
 
output file "FileInputOutput.app"
 
CFURLRef ParentDirectory // Create a url for the desktop
ParentDirectory = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
 
CFURLRef inputURL // Create a url for input.txt on the desktop
inputURL = fn URLByAppendingPathComponent( ParentDirectory, @"input.txt" )
 
CFURLRef outputURL // Create a url for output.txt on the desktop
outputURL = fn URLByAppendingPathComponent( ParentDirectory, @"output.txt" )
 
open "O", 1, outputURL
open "I", 2, inputURL
 
str255 dataLine
 
While Not Eof(2)
Line Input #2, dataLine
Print #1, dataLine
Wend
 
Close #1
Close #2
 
end
 
</syntaxhighlight>
 
=={{header|Gambas}}==
<syntaxhighlight lang="gambas">Public Sub Main()
Dim sOutput As String = "Hello "
Dim sInput As String = File.Load(User.Home &/ "input.txt") 'Has the word 'World!' stored
 
File.Save(User.Home &/ "output.txt", sOutput)
File.Save(User.Home &/ "input.txt", sOutput & sInput)
 
Print "'input.txt' contains - " & sOutput & sInput
Print "'output.txt' contains - " & sOutput
 
End</syntaxhighlight>
Output:
<pre>
'input.txt' contains - Hello World!
'output.txt' contains - Hello
</pre>
 
=={{header|GAP}}==
Line 2,020 ⟶ 2,197:
 
=={{header|GML}}==
 
<syntaxhighlight lang="gml">var file, str;
file = file_text_open_read("input.txt");
Line 2,393 ⟶ 2,569:
 
'output.txt 'input.txt copy-file</syntaxhighlight>
 
=={{header|Liberty BASIC}}==
<syntaxhighlight lang="lb">nomainwin
 
open "input.txt" for input as #f1
qtyBytes = lof( #f1)
source$ = input$( #f1, qtyBytes)
close #f1
 
open "output.txt" for output as #f2
#f2 source$;
close #f2
 
end</syntaxhighlight>
 
=={{header|Lingo}}==
Line 3,089 ⟶ 3,251:
Using an alternate cmdlet to write the file
<syntaxhighlight lang="powershell">Get-Content $PWD\input.txt | Set-Content $PWD\output.txt</syntaxhighlight>
 
=={{header|PureBasic}}==
 
 
Basic file copy
<syntaxhighlight lang="purebasic">CopyFile("input.txt","output.txt")</syntaxhighlight>
 
 
Line by line
<syntaxhighlight lang="purebasic">in = ReadFile(#PB_Any,"input.txt")
If in
out = CreateFile(#PB_Any,"output.txt")
If out
Define MyLine$
While Not Eof(in)
MyLine$ = ReadString(in)
WriteString(out,MyLine$)
Wend
CloseFile(out)
EndIf
CloseFile(in)
EndIf</syntaxhighlight>
 
 
Reading & writing the complete file in one pass
<syntaxhighlight lang="purebasic">If ReadFile(0,"input.txt")
Define MyLine$, *Buffer, length
length=FileSize("input.txt")
*Buffer = AllocateMemory(length)
If *Buffer
If OpenFile(1,"output.txt")
ReadData(0, *Buffer, length)
WriteData(1, *Buffer, length)
CloseFile(1)
EndIf
FreeMemory(*Buffer)
EndIf
CloseFile(0)
EndIf</syntaxhighlight>
 
=={{header|Python}}==
 
 
The following use of the standard libraries shutil.copyfile is to be preferred. (Current source code ensures that failure to open files raises appropriate exceptions, a restricted buffer is used to copy the files using binary mode, and any used file descriptors are always closed).
 
Line 3,252 ⟶ 3,373:
$in.close;
$out.close;</syntaxhighlight>
 
=={{header|RapidQ}}==
 
File I/O is one of the things where RapidQ differs from standard Basic. RapidQ uses file streams.
 
The first version copies text line by line, as in the ''BASIC'' example.
 
<syntaxhighlight lang="rapidq">$INCLUDE "rapidq.inc"
 
DIM File1 AS QFileStream
DIM File2 AS QFileStream
 
File1.Open("input.txt", fmOpenRead)
File2.Open("output.txt", fmCreate)
 
WHILE NOT File1.EOF
data$ = File1.ReadLine
File2.WriteLine(data$)
WEND
 
File1.Close
File2.Close</syntaxhighlight>
 
When just copying data, the code can be simplified by using the CopyFrom method.<br />
(The second parameter for CopyFrom is number of bytes to copy, 0 = copy the whole file.)
 
<syntaxhighlight lang="rapidq">$INCLUDE "rapidq.inc"
 
DIM File1 AS QFileStream
DIM File2 AS QFileStream
 
File1.Open("input.txt", fmOpenRead)
File2.Open("output.txt", fmCreate)
 
File2.CopyFrom(File1, 0)
 
File1.Close
File2.Close</syntaxhighlight>
 
=={{header|Raven}}==
<syntaxhighlight lang="raven">'input.txt' read 'output.txt' write</syntaxhighlight>
 
=={{header|REALbasic}}==
<syntaxhighlight lang="vb">
Sub WriteToFile(input As FolderItem, output As FolderItem)
Dim tis As TextInputStream
Dim tos As TextOutputStream
tis = tis.Open(input)
tos = tos.Create(output)
While Not tis.EOF
tos.WriteLine(tis.ReadLine)
Wend
tis.Close
tos.Close
End Sub
</syntaxhighlight>
 
=={{header|REBOL}}==
Line 3,402 ⟶ 3,470:
FileUtils.copy_file 'input.txt', 'output.txt'</syntaxhighlight>
 
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
fileLen = LOF(#in) 'Length Of File
Line 3,412 ⟶ 3,481:
close #out
end
</syntaxhighlight>
 
' orOr directly with no intermediate fileData$
{{works with|Just BASIC}}
 
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
open "output.txt" for output as #out
fileLen = LOF(#in) 'Length Of File
Line 3,760 ⟶ 3,829:
 
fileio = ~command.files; &h.path.&h:= 'output.txt'!</syntaxhighlight>
 
=={{header|VBA}}==
<syntaxhighlight lang="vb">Option Explicit
 
Sub Main()
Dim s As String, FF As Integer
 
'read a file line by line
FF = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\input.txt" For Input As #FF
While Not EOF(FF)
Line Input #FF, s
Debug.Print s
Wend
Close #FF
 
'read a file
FF = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\input.txt" For Input As #FF
s = Input(LOF(1), #FF)
Close #FF
Debug.Print s
 
'write a file
FF = FreeFile
Open "C:\Users\" & Environ("username") & "\Desktop\output.txt" For Output As #FF
Print #FF, s
Close #FF
End Sub</syntaxhighlight>
 
=={{header|VBScript}}==
one liner (-2 for system default encoding)
<syntaxhighlight lang="vb">CreateObject("Scripting.FileSystemObject").OpenTextFile("output.txt",2,-2).Write CreateObject("Scripting.FileSystemObject").OpenTextFile("input.txt", 1, -2).ReadAll</syntaxhighlight>
 
=={{header|Vedit macro language}}==
Line 3,800 ⟶ 3,836:
File_Save_As("output.txt", NOMSG)
Buf_Close(NOMSG) </syntaxhighlight>
 
=={{header|Visual Basic .NET}}==
{{works with|Visual Basic .NET|9.0+}}
 
<syntaxhighlight lang="vbnet">'byte copy
My.Computer.FileSystem.WriteAllBytes("output.txt", _
My.Computer.FileSystem.ReadAllBytes("input.txt"), False)
'text copy
Using input = IO.File.OpenText("input.txt"), _
output As New IO.StreamWriter(IO.File.OpenWrite("output.txt"))
output.Write(input.ReadToEnd)
End Using
'Line by line text copy
Using input = IO.File.OpenText("input.txt"), _
output As New IO.StreamWriter(IO.File.OpenWrite("output.txt"))
Do Until input.EndOfStream
output.WriteLine(input.ReadLine)
Loop
End Using</syntaxhighlight>
 
=={{header|Wart}}==
512

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.