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

Rename Perl 6 -> Raku, alphabetize, minor clean-up
(Rename Perl 6 -> Raku, alphabetize, minor clean-up)
Line 103:
4 records
</pre>
 
=={{header|Batch File}}==
<lang dos>
Line 209 ⟶ 210:
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|C sharp|C#}}==
<lang csharp>using System;
using System.IO;
 
namespace AppendPwdRosetta
{
class PasswordRecord
{
public string account, password, fullname, office, extension, homephone, email, directory, shell;
public int UID, GID;
public PasswordRecord(string account, string password, int UID, int GID, string fullname, string office, string extension, string homephone,
string email, string directory, string shell)
{
this.account = account; this.password = password; this.UID = UID; this.GID = GID; this.fullname = fullname; this.office = office;
this.extension = extension; this.homephone = homephone; this.email = email; this.directory = directory; this.shell = shell;
}
public override string ToString()
{
var gecos = string.Join(",", new string[] { fullname, office, extension, homephone, email });
return string.Join(":", new string[] { account, password, UID.ToString(), GID.ToString(), gecos, directory, shell });
}
}
class Program
{
static void Main(string[] args)
{
var jsmith = new PasswordRecord("jsmith", "x", 1001, 1000, "Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org",
"/home/jsmith", "/bin/bash");
var jdoe = new PasswordRecord("jdoe", "x", 1002, 1000, "Jane Doe", "Room 1004", "(234)555-8914", "(234)555-0044", "jdoe@rosettacode.org", "/home/jdoe",
"/bin/bash");
var xyz = new PasswordRecord("xyz", "x", 1003, 1000, "X Yz", "Room 1003", "(234)555-8913", "(234)555-0033", "xyz@rosettacode.org", "/home/xyz", "/bin/bash");
 
// Write these records out in the typical system format.
File.WriteAllLines("passwd.txt", new string[] { jsmith.ToString(), jdoe.ToString() });
 
// Append a new record to the file and close the file again.
File.AppendAllText("passwd.txt", xyz.ToString());
 
// Open the file and demonstrate the new record has indeed written to the end.
string[] lines = File.ReadAllLines("passwd.txt");
Console.WriteLine("Appended record: " + lines[2]);
}
}
}
</lang>
 
{{out}}
<pre>>AppendPwdRosetta.exe
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|C++}}==
Line 295 ⟶ 346:
{{out}}
<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|C sharp|C#}}==
<lang csharp>using System;
using System.IO;
 
namespace AppendPwdRosetta
{
class PasswordRecord
{
public string account, password, fullname, office, extension, homephone, email, directory, shell;
public int UID, GID;
public PasswordRecord(string account, string password, int UID, int GID, string fullname, string office, string extension, string homephone,
string email, string directory, string shell)
{
this.account = account; this.password = password; this.UID = UID; this.GID = GID; this.fullname = fullname; this.office = office;
this.extension = extension; this.homephone = homephone; this.email = email; this.directory = directory; this.shell = shell;
}
public override string ToString()
{
var gecos = string.Join(",", new string[] { fullname, office, extension, homephone, email });
return string.Join(":", new string[] { account, password, UID.ToString(), GID.ToString(), gecos, directory, shell });
}
}
class Program
{
static void Main(string[] args)
{
var jsmith = new PasswordRecord("jsmith", "x", 1001, 1000, "Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org",
"/home/jsmith", "/bin/bash");
var jdoe = new PasswordRecord("jdoe", "x", 1002, 1000, "Jane Doe", "Room 1004", "(234)555-8914", "(234)555-0044", "jdoe@rosettacode.org", "/home/jdoe",
"/bin/bash");
var xyz = new PasswordRecord("xyz", "x", 1003, 1000, "X Yz", "Room 1003", "(234)555-8913", "(234)555-0033", "xyz@rosettacode.org", "/home/xyz", "/bin/bash");
 
// Write these records out in the typical system format.
File.WriteAllLines("passwd.txt", new string[] { jsmith.ToString(), jdoe.ToString() });
 
// Append a new record to the file and close the file again.
File.AppendAllText("passwd.txt", xyz.ToString());
 
// Open the file and demonstrate the new record has indeed written to the end.
string[] lines = File.ReadAllLines("passwd.txt");
Console.WriteLine("Appended record: " + lines[2]);
}
}
}
</lang>
 
{{out}}
<pre>>AppendPwdRosetta.exe
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|COBOL}}==
Line 2,084 ⟶ 2,085:
|}
Note that flock uses advisory lock; some other program (if it doesn't use flock) can still unexpectedly write to the file.
 
=={{header|Perl 6}}==
{{works with|Rakudo|2017.09}}
 
This is kind of silly as it takes a string, converts it to a record, and then instantly converts it back to a string to write out to a file. Most of the "record handling" code is just demonstrating a possible way to store records in memory. It really has nothing to do with appending a string to a file.
 
<lang perl6>class record {
has $.name;
has $.password;
has $.UID;
has $.GID;
has $.fullname;
has $.office;
has $.extension;
has $.homephone;
has $.email;
has $.directory;
has $.shell;
 
method gecos { join ',', $.fullname, $.office, $.extension, $.homephone, $.email }
 
method gist {
join ':',
$.name,
$.password,
$.UID,
$.GID,
self.gecos,
$.directory,
$.shell;
}
};
 
my $fname = 'foo.fil';
 
given $fname.IO.open(:w) { .close }; # clear file
 
sub append ($file, $line){
my $fh = $file.IO.open(:a) or fail "Unable to open $file";
given $fh {
# Get a lock on the file, waits until lock is active
.lock;
# seek to the end in case some other process wrote to
# the file while we were waiting for the lock
.seek(0, SeekType::SeekFromEnd);
# write the record
.say: $line;
.close;
}
}
 
sub str-to-record ($str) {
my %rec = <name password UID GID fullname office extension
homephone email directory shell> Z=> $str.split(/<[:,]>/);
my $user = record.new(|%rec);
}
 
for
'jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash',
'jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash'
-> $line {
my $thisuser = str-to-record $line;
$fname.&append: $thisuser.gist;
}
 
put "Last line of $fname before append:";
put $fname.IO.lines.tail;
 
$fname.&append: str-to-record('xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash').gist;
 
put "Last line of $fname after append:";
put $fname.IO.lines.tail;
</lang>
{{out}}
<pre>Last line of foo.fil before append:
jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash
Last line of foo.fil after append:
xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/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
|-
| Class || Text file || Whichever is provided by<BR>the underlying VM || ☑ || ☑ || ☑ Advisory lock<BR>Depends on OS and VM
|}
Note that advisory locks do not prevent some other program (if it doesn't use flock) from unexpectedly writing to the file.
 
=={{header|Phix}}==
Line 2,773 ⟶ 2,680:
=> ("xyz" "x" 1003 1000 ("X Yz" "Room 1003" "(234)555-8913" "(234)555-0033" "xyz@rosettacode.org") "/home/xyz" "/bin/bash")
</pre>
 
=={{header|Raku}}==
(formerly Perl 6)
{{works with|Rakudo|2017.09}}
 
This is kind of silly as it takes a string, converts it to a record, and then instantly converts it back to a string to write out to a file. Most of the "record handling" code is just demonstrating a possible way to store records in memory. It really has nothing to do with appending a string to a file.
 
<lang perl6>class record {
has $.name;
has $.password;
has $.UID;
has $.GID;
has $.fullname;
has $.office;
has $.extension;
has $.homephone;
has $.email;
has $.directory;
has $.shell;
 
method gecos { join ',', $.fullname, $.office, $.extension, $.homephone, $.email }
 
method gist {
join ':',
$.name,
$.password,
$.UID,
$.GID,
self.gecos,
$.directory,
$.shell;
}
};
 
my $fname = 'foo.fil';
 
given $fname.IO.open(:w) { .close }; # clear file
 
sub append ($file, $line){
my $fh = $file.IO.open(:a) or fail "Unable to open $file";
given $fh {
# Get a lock on the file, waits until lock is active
.lock;
# seek to the end in case some other process wrote to
# the file while we were waiting for the lock
.seek(0, SeekType::SeekFromEnd);
# write the record
.say: $line;
.close;
}
}
 
sub str-to-record ($str) {
my %rec = <name password UID GID fullname office extension
homephone email directory shell> Z=> $str.split(/<[:,]>/);
my $user = record.new(|%rec);
}
 
for
'jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash',
'jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash'
-> $line {
my $thisuser = str-to-record $line;
$fname.&append: $thisuser.gist;
}
 
put "Last line of $fname before append:";
put $fname.IO.lines.tail;
 
$fname.&append: str-to-record('xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash').gist;
 
put "Last line of $fname after append:";
put $fname.IO.lines.tail;
</lang>
{{out}}
<pre>Last line of foo.fil before append:
jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash
Last line of foo.fil after append:
xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/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
|-
| Class || Text file || Whichever is provided by<BR>the underlying VM || ☑ || ☑ || ☑ Advisory lock<BR>Depends on OS and VM
|}
Note that advisory locks do not prevent some other program (if it doesn't use flock) from unexpectedly writing to the file.
 
=={{header|RapidQ}}==
Line 3,095 ⟶ 3,097:
xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash
</pre>
 
=={{header|Rust}}==
<lang rust>
10,327

edits