Append a record to the end of a text file: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
Rename Perl 6 -> Raku, alphabetize, minor clean-up
PureFox (talk | contribs)
Added Wren
Line 3,728:
<pre>Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
 
=={{header|Wren}}==
Although Wren has basic classes to work with the file system, these are only available when creating standalone scripts using the command line interpreter (CLI) and not when Wren is being embedded. In the latter case it relies on the host application for such support.
 
Currently, in scripts written for the CLI, there appears to be no easy way to append something to an existing file.
 
The work-around is to read the contents of the file (if large it can be read in chunks) and write them to a new file together with whatever needs to be appended. One then needs to delete the existing file and rename the new one.
 
However, to avoid the latter steps, a file can be created using an existing name - it simply truncates the existing contents before anything is written - so care obviously needs to be taken when using this facility.
<lang ecmascript>import "io" for File
 
var records = [
"jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,[email protected]:/home/jsmith:/bin/bash",
"jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,[email protected]:/home/jdoe:/bin/bash"
]
 
// Write records to a new file called "passwd.csv" and close it.
var fileName = "passwd.csv"
File.create(fileName) {|file|
records.each { |r| file.writeBytes(r + "\n") }
}
 
// Check file has been created correctly.
var contents = File.read(fileName)
System.print("Initial records:\n")
System.print(contents)
 
var newRec = "xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,[email protected]:/home/xyz:/bin/bash"
 
// Append the new record to the file and close it.
File.create(fileName) {|file|
file.writeBytes(contents)
file.writeBytes(newRec + "\n")
}
 
// Check the new record has been appended correctly.
contents = File.read(fileName)
System.print("Records after appending new one:\n")
System.print(contents)</lang>
 
{{out}}
<pre>
Initial records:
 
jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,[email protected]:/home/jsmith:/bin/bash
jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,[email protected]:/home/jdoe:/bin/bash
 
Records after appending new one:
 
jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,[email protected]:/home/jsmith:/bin/bash
jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,[email protected]:/home/jdoe:/bin/bash
xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,[email protected]:/home/xyz:/bin/bash
</pre>
 
{|class="wikitable" style="text-align: center; margin: 1em auto 1em auto;"
|+ Append Capabilities.
|-
!colspan=2| Data Representation
!rowspan=2| IO<BR>Library
!rowspan=2| Append<BR>Possible
!rowspan=2| Automatic<BR>Append
!rowspan=2| Multi-tasking<BR>Safe
|-
! In core || On disk
|-
| Wren string / class || CSV text file || Wren io module / libuv || ☑ || ☒ || ☑ (AFAIK)
|}
<br>
=={{header|Yabasic}}==
<lang Yabasic>a = open("passwd", "a") // Open the file for appending, i.e. what you write to the file will be appended after its initial contents.