How to resolve the algorithm Append a record to the end of a text file step by step in the zkl programming language
How to resolve the algorithm Append a record to the end of a text file step by step in the zkl 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 zkl programming language
Source code in the zkl programming language
var [const]
gnames=T("fullname","office","extension","homephone","email"),
pnames=T("account","password","uid","gid","gecos","directory","shell");
class Gecos{
var fullname, office, extension, homephone, email;
fcn init(str){ gnames.zipWith(setVar,vm.arglist) }
fcn toString { gnames.apply(setVar).concat(",") }
}
class Passwd{
var account,password,uid,gid,gecos,directory,shell;
fcn init(str){ pnames.zipWith(setVar,vm.arglist) }
fcn toString { pnames.apply(setVar).concat(":") }
}
fcn strToPasswd(str){ // blow apart file line to class
p:=str.strip().split(":");
g:=Gecos(p[4].split(",").xplode());
Passwd(p[0,4].xplode(),g,p[5,*].xplode());
}
jsmith:=Passwd("jsmith","x",1001, 1000,
Gecos("Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org"),
"/home/jsmith", "/bin/bash");
jdoe:=strToPasswd("jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,"
"jdoe@rosettacode.org:/home/jdoe:/bin/bash");
xyz:=strToPasswd("xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,"
"xyz@rosettacode.org:/home/xyz:/bin/bash");
filename:="append.records.test";
f:=File(filename,"w"); // create file with 2 records
f.writeln(jsmith,"\n",jdoe); f.close();
f:=File(filename,"a+"); // append a third record
f.writeln(xyz); f.close();
File(filename).read().text.print(); // print file
You may also check:How to resolve the algorithm Apply a digital filter (direct form II transposed) step by step in the C++ programming language
You may also check:How to resolve the algorithm Rep-string step by step in the zkl programming language
You may also check:How to resolve the algorithm Time a function step by step in the Batch File programming language
You may also check:How to resolve the algorithm Get system command output step by step in the Swift programming language
You may also check:How to resolve the algorithm Numerical integration step by step in the Groovy programming language