Task: Append a record to the end of a text file







Python: 



passwd_list=[
  dict(account='jsmith', password='x', UID=1001, GID=1000, 
       GECOS=dict(fullname='Joe Smith', office='Room 1007', extension='(234)555-8917',
                  homephone='(234)555-0077', email='jsmith@rosettacode.org'),
                  directory='/home/jsmith', shell='/bin/bash'),
  dict(account='jdoe', password='x', UID=1002, GID=1000,
       GECOS=dict(fullname='Jane Doe', office='Room 1004', extension='(234)555-8914',
                  homephone='(234)555-0044', email='jdoe@rosettacode.org'),
       directory='/home/jdoe', shell='/bin/bash')
]

passwd_fields="account password UID GID GECOS directory shell".split()
GECOS_fields="fullname office extension homephone email".split()

def passwd_text_repr(passwd_rec):

  passwd_rec["GECOS"]=",".join([ passwd_rec["GECOS"][field] for field in GECOS_fields])
  for field in passwd_rec: 
    if not isinstance(passwd_rec[field], str):
      passwd_rec[field]=`passwd_rec[field]`
  return ":".join([ passwd_rec[field] for field in passwd_fields ])

passwd_text=open("passwd.txt","w")
for passwd_rec in passwd_list:
  print >> passwd_text,passwd_text_repr(passwd_rec)
passwd_text.close()




passwd_text=open("passwd.txt","a+")
new_rec=dict(account='xyz', password='x', UID=1003, GID=1000,
             GECOS=dict(fullname='X Yz', office='Room 1003', extension='(234)555-8913',
                        homephone='(234)555-0033', email='xyz@rosettacode.org'),
             directory='/home/xyz', shell='/bin/bash')
print >> passwd_text,  passwd_text_repr(new_rec)
passwd_text.close()




passwd_list=list(open("passwd.txt","r"))
if "xyz" in passwd_list[-1]:
  print "Appended record:",passwd_list[-1][:-1]




Fortran:       PROGRAM DEMO	
      TYPE DETAILS		
       CHARACTER*28 FULLNAME
       CHARACTER*12 OFFICE
       CHARACTER*16 EXTENSION
       CHARACTER*16 HOMEPHONE
       CHARACTER*88 EMAIL
      END TYPE DETAILS
      TYPE USERSTUFF		
       CHARACTER*8 ACCOUNT
       CHARACTER*8 PASSWORD	
       INTEGER*2 UID
       INTEGER*2 GID
       TYPE(DETAILS) PERSON
       CHARACTER*18 DIRECTORY
       CHARACTER*12 SHELL
      END TYPE USERSTUFF
      TYPE(USERSTUFF) NOTE	
      NAMELIST /STUFF/ NOTE	
      INTEGER F,MSG,N
      MSG = 6	
      F = 10	
      OPEN(MSG, DELIM = "QUOTE")	

Create the file and supply its initial content.
      OPEN (F, FILE="Staff.txt",STATUS="REPLACE",ACTION="WRITE",
     1 DELIM="QUOTE",RECL=666)	

      WRITE (F,*) USERSTUFF("jsmith","x",1001,1000,
     1 DETAILS("Joe Smith","Room 1007","(234)555-8917",
     2  "(234)555-0077","jsmith@rosettacode.org"),
     2 "/home/jsmith","/bin/bash")

      WRITE (F,*) USERSTUFF("jdoe","x",1002,1000,
     1 DETAILS("Jane Doe","Room 1004","(234)555-8914",
     2  "(234)555-0044","jdoe@rosettacode.org"),
     3 "home/jdoe","/bin/bash")
      CLOSE (F)		

Choose the existing file, and append a further record to it.
      OPEN (F, FILE="Staff.txt",STATUS="OLD",ACTION="WRITE",
     1 DELIM="QUOTE",RECL=666,ACCESS="APPEND")

      NOTE = USERSTUFF("xyz","x",1003,1000,		
     1 DETAILS("X Yz","Room 1003","(234)555-8193",
     2  "(234)555-033","xyz@rosettacode.org"),
     3 "/home/xyz","/bin/bash")
      WRITE (F,*) NOTE					
      CLOSE (F)

Chase through the file, revealing what had been written..
      OPEN (F, FILE="Staff.txt",STATUS="OLD",ACTION="READ",
     1 DELIM="QUOTE",RECL=666)
      N = 0
   10 READ (F,*,END = 20) NOTE	
      N = N + 1			
      WRITE (MSG,11) N		
   11 FORMAT (/,"Record ",I0)	
      WRITE (MSG,STUFF)		
      GO TO 10			

Closedown.
   20 CLOSE (F)
      END