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

C: #include <stdio.h>
#include <string.h>

typedef const char *STRING;
typedef struct{STRING fullname, office, extension, homephone, email; } gecos_t;
typedef struct{STRING account, password; int uid, gid; gecos_t gecos; STRING directory, shell; } passwd_t;

#define GECOS_FMT "%s,%s,%s,%s,%s"
#define PASSWD_FMT "%s:%s:%d:%d:"GECOS_FMT":%s:%s"

passwd_t passwd_list[]={
  {"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"}
};

main(){

  FILE *passwd_text=fopen("passwd.txt", "w");
  int rec_num;
  for(rec_num=0; rec_num < sizeof passwd_list/sizeof(passwd_t); rec_num++)
    fprintf(passwd_text, PASSWD_FMT"\n", passwd_list[rec_num]);
  fclose(passwd_text);


  passwd_text=fopen("passwd.txt", "a+");
  char passwd_buf[BUFSIZ]; 
  passwd_t new_rec =
      {"xyz", "x", 1003, 1000, 
          {"X Yz", "Room 1003", "(234)555-8913", "(234)555-0033", "xyz@rosettacode.org"},
          "/home/xyz", "/bin/bash"};
  sprintf(passwd_buf, PASSWD_FMT"\n", new_rec);

  write(fileno(passwd_text), passwd_buf, strlen(passwd_buf));
  close(passwd_text);


  passwd_text=fopen("passwd.txt", "r");
  while(!feof(passwd_text))
    fscanf(passwd_text, "%[^\n]\n", passwd_buf, "\n");
  if(strstr(passwd_buf, "xyz"))
    printf("Appended record: %s\n", passwd_buf);
}










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