How to resolve the algorithm Append a record to the end of a text file step by step in the Sidef programming language
How to resolve the algorithm Append a record to the end of a text file step by step in the Sidef programming language
Table of Contents
Problem Statement
Many systems offer the ability to open a file for writing, such that any data written will be appended to the end of the file. Further, the file operations will always adjust the position pointer to guarantee the end of the file, even in a multitasking environment. This feature is most useful in the case of log files, where many jobs may be appending to the log file at the same time, or where care must be taken to avoid concurrently overwriting the same record from another job.
Given a two record sample for a mythical "passwd" file: Resulting file format: should mimic Linux's /etc/passwd file format with particular attention to the "," separator used in the GECOS field. But if the specific language has a particular or unique format of storing records in text file, then this format should be named and demonstrated with an additional example. Expected output: Finally: Provide a summary of the language's "append record" capabilities in a table. eg. Alternatively: If the language's appends can not guarantee its writes will always append, then note this restriction in the table. If possible, provide an actual code example (possibly using file/record locking) to guarantee correct concurrent appends.
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Append a record to the end of a text file step by step in the Sidef programming language
Source code in the sidef programming language
define (
RECORD_FIELDS = %w(account password UID GID GECOS directory shell),
GECOS_FIELDS = %w(fullname office extension homephone email),
RECORD_SEP = ':',
GECOS_SEP = ',',
PASSWD_FILE = 'passwd.txt',
)
# here's our three records
var records_to_write = [
Hash(
account => 'jsmith',
password => 'x',
UID => 1001,
GID => 1000,
GECOS => Hash(
fullname => 'John Smith',
office => 'Room 1007',
extension => '(234)555-8917',
homephone => '(234)555-0077',
email => 'jsmith@rosettacode.org',
),
directory => '/home/jsmith',
shell => '/bin/bash',
),
Hash(
account => 'jdoe',
password => 'x',
UID => 1002,
GID => 1000,
GECOS => Hash(
fullname => 'Jane Doe',
office => 'Room 1004',
extension => '(234)555-8914',
homephone => '(234)555-0044',
email => 'jdoe@rosettacode.org',
),
directory => '/home/jdoe',
shell => '/bin/bash',
),
];
var record_to_append = Hash(
account => 'xyz',
password => 'x',
UID => 1003,
GID => 1000,
GECOS => Hash(
fullname => 'X Yz',
office => 'Room 1003',
extension => '(234)555-8913',
homephone => '(234)555-0033',
email => 'xyz@rosettacode.org',
),
directory => '/home/xyz',
shell => '/bin/bash',
);
func record_to_string(rec, sep = RECORD_SEP, fields = RECORD_FIELDS) {
gather {
fields.each { |field|
var r = rec{field} \\ die "Field #{field} not found"
take(field == 'GECOS' ? record_to_string(r, GECOS_SEP, GECOS_FIELDS)
: r)
}
}.join(sep)
}
func write_records_to_file(records, filename = PASSWD_FILE, append = false) {
File(filename).(append ? :open_a : :open_w)(\var fh, \var err)
err && die "Can't open #{filename}: #{err}";
fh.flock(File.LOCK_EX) || die "Can't lock #{filename}: $!"
fh.seek(0, File.SEEK_END) || die "Can't seek #{filename}: $!"
records.each { |record| fh.say(record_to_string(record)) }
fh.flock(File.LOCK_UN) || die "Can't unlock #{filename}: $!"
fh.close
}
# write two records to file
write_records_to_file(records: records_to_write);
# append one more record to file
write_records_to_file(records: [record_to_append], append: true);
# test
File(PASSWD_FILE).open_r(\var fh, \var err)
err && die "Can't open file #{PASSWD_FILE}: #{err}"
var lines = fh.lines
# There should be more than one line
assert(lines.len > 1)
# Check the last line
assert_eq(lines[-1], 'xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,' +
'(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash')
say "** Test passed!"
You may also check:How to resolve the algorithm First-class functions step by step in the Elixir programming language
You may also check:How to resolve the algorithm K-d tree step by step in the Julia programming language
You may also check:How to resolve the algorithm Roots of a function step by step in the Go programming language
You may also check:How to resolve the algorithm Square-free integers step by step in the AWK programming language
You may also check:How to resolve the algorithm FizzBuzz step by step in the Nu programming language