File input/output: Difference between revisions

Content added Content deleted
(Dialects of BASIC moved to the BASIC section.)
Line 847: Line 847:
close 1
close 1
close 2</syntaxhighlight>
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}}===
==={{header|Commodore BASIC}}===
Line 862: Line 874:
120 close 8
120 close 8
130 end</syntaxhighlight>
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}}===
==={{header|IS-BASIC}}===
Line 878: Line 985:
220 CLOSE #2
220 CLOSE #2
230 END HANDLER</syntaxhighlight>
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}}===
==={{header|QBasic}}===
Line 889: Line 1,046:
CLOSE #2
CLOSE #2
END</syntaxhighlight>
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}}===
==={{header|True BASIC}}===
Line 901: Line 1,133:
CLOSE #2
CLOSE #2
END</syntaxhighlight>
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}}===
==={{header|Yabasic}}===
Line 920: Line 1,206:


There may be other techniques too.
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}}==
=={{header|BCPL}}==
Line 1,896: Line 2,170:


end program FileIO</syntaxhighlight>
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}}==
=={{header|Frink}}==
Line 1,938: Line 2,178:
w.close[]
w.close[]
</syntaxhighlight>
</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}}==
=={{header|GAP}}==
Line 2,020: Line 2,197:


=={{header|GML}}==
=={{header|GML}}==

<syntaxhighlight lang="gml">var file, str;
<syntaxhighlight lang="gml">var file, str;
file = file_text_open_read("input.txt");
file = file_text_open_read("input.txt");
Line 2,393: Line 2,569:


'output.txt 'input.txt copy-file</syntaxhighlight>
'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}}==
=={{header|Lingo}}==
Line 3,089: Line 3,251:
Using an alternate cmdlet to write the file
Using an alternate cmdlet to write the file
<syntaxhighlight lang="powershell">Get-Content $PWD\input.txt | Set-Content $PWD\output.txt</syntaxhighlight>
<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}}==
=={{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).
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: Line 3,373:
$in.close;
$in.close;
$out.close;</syntaxhighlight>
$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}}==
=={{header|Raven}}==
<syntaxhighlight lang="raven">'input.txt' read 'output.txt' write</syntaxhighlight>
<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}}==
=={{header|REBOL}}==
Line 3,402: Line 3,470:
FileUtils.copy_file 'input.txt', 'output.txt'</syntaxhighlight>
FileUtils.copy_file 'input.txt', 'output.txt'</syntaxhighlight>


=={{header|Run BASIC}}==
==={{header|Run BASIC}}===
{{works with|Just BASIC}}
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
fileLen = LOF(#in) 'Length Of File
fileLen = LOF(#in) 'Length Of File
Line 3,412: Line 3,481:
close #out
close #out
end
end
</syntaxhighlight>

' or directly with no intermediate fileData$
Or directly with no intermediate fileData$
{{works with|Just BASIC}}

open "input.txt" for input as #in
<syntaxhighlight lang="runbasic">open "input.txt" for input as #in
open "output.txt" for output as #out
open "output.txt" for output as #out
fileLen = LOF(#in) 'Length Of File
fileLen = LOF(#in) 'Length Of File
Line 3,760: Line 3,829:


fileio = ~command.files; &h.path.&h:= 'output.txt'!</syntaxhighlight>
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}}==
=={{header|Vedit macro language}}==
Line 3,800: Line 3,836:
File_Save_As("output.txt", NOMSG)
File_Save_As("output.txt", NOMSG)
Buf_Close(NOMSG) </syntaxhighlight>
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}}==
=={{header|Wart}}==