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.



Task

Given a two record sample for a mythical "passwd" file:

 Write these records out in the typical system format.
 Ideally these records will have named fields of various types.
 Close the file, then reopen the file for append.
 Append a new record to the file and close the file again.
 Take appropriate care to avoid concurrently overwrites from another job.
 Open the file and demonstrate the new record has indeed written to the end.

 Source record field types and contents.


account
password
UID
GID
fullname,office,extension,homephone,email
directory
shell


string
string
int
int
struct(string,string,string,string,string)
string
string


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


 Record to be appended.


account
password
UID
GID
fullname,office,extension,homephone,email
directory
shell


string
string
int
int
struct(string,string,string,string,string)
string
string


xyz
x
1003
1000
X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org
/home/xyz
/bin/bash

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:

Appended record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash

Finally: Provide a summary of the language's "append record" capabilities in a table. eg.


 Append Capabilities.


 Data Representation

 IO
Library

 Append
Possible

 Automatic
Append

 Multi-tasking
Safe


 In core 
 On disk


 C struct 
 CSV text file 
 glibc/stdio 
 ☑ 
 ☑ 
 ☑ (Not all, eg NFS)

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.




/*REXX program  writes (appends) two records,  closes the file,  appends another record.*/
tFID= 'PASSWD.TXT'                               /*define the name of the  output  file.*/
call lineout tFID                                /*close the output file,  just in case,*/
                                                 /*   it could be open from calling pgm.*/
call writeRec tFID,,                             /*append the  1st record  to the file. */
   'jsmith',"x", 1001, 1000, 'Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org', "/home/jsmith", '/bin/bash'
 
call writeRec tFID,,                             /*append the  2nd record  to the file. */
   'jdoe',  "x", 1002, 1000, 'Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org',    "/home/jsmith", '/bin/bash'
 
call lineout fid                                 /*close the outfile  (just to be tidy).*/
 
call writeRec tFID,,                             /*append the  3rd record  to the file. */
   'xyz',   "x", 1003, 1000, 'X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org',         "/home/xyz",    '/bin/bash'
/*─account─pw────uid───gid──────────────fullname,office,extension,homephone,Email────────────────────────directory───────shell──*/
 
call lineout fid                                 /*"be safe" programming: close the file*/
exit                                             /*stick a fork in it,  we're all done. */
/*──────────────────────────────────────────────────────────────────────────────────────*/
s:   if arg(1)==1  then return arg(3);       return word(arg(2) 's', 1)     /*pluralizer*/
/*──────────────────────────────────────────────────────────────────────────────────────*/
writeRec: parse arg fid,_                        /*get the fileID, and also the 1st arg.*/
          sep=':'                                /*field delimiter used in file, it ··· */
                                                 /*      ··· can be unique and any size.*/
                       do i=3  to arg()          /*get each argument and append it to   */
                       _=_ || sep || arg(i)      /*  the previous arg, with a   :   sep.*/
                       end   /*i*/
 
                       do tries=0  for 11        /*keep trying for  66  seconds.        */
                       r=lineout(fid, _)         /*write (append)  the new record.      */
                       if r==0  then return      /*Zero?   Then record was written.     */
                       call sleep tries          /*Error?  So try again after a delay.  */
                       end   /*tries*/           /*Note:  not all REXXes have  SLEEP.   */
 
          say '***error***';  say r  'record's(r)   "not written to file"   fid;   exit 13
          /*some error causes: no write access, disk is full, file lockout, no authority*/