How to resolve the algorithm Append a record to the end of a text file step by step in the FreeBASIC programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Append a record to the end of a text file step by step in the FreeBASIC 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 FreeBASIC programming language

Source code in the freebasic programming language

' FB 1.05.0 Win64

Type Person
  As String  fullname
  As String  office
  As String  extension
  As String  homephone
  As String  email
  Declare Constructor()
  Declare Constructor(As String, As String, As String, As String, As String)
  Declare Operator Cast() As String
End Type

Constructor Person()
End Constructor

Constructor Person(fullname As String, office As String, extension As String, _
homephone As String, email As String)

  With This
    .fullname  = fullname
    .office    = office
    .extension = extension
    .homephone = homephone
    .email     = email
  End With

End Constructor

Operator Person.Cast() As String
  Return fullname + "," + office + "," + extension + "," + homephone + "," + email
End Operator

Type Record
  As String  account
  As String  password
  As Integer uid
  As Integer gid
  As Person  user
  As String  directory
  As String  shell
  Declare Constructor() 
  Declare Constructor(As String, As String, As Integer, As Integer, As Person, As String, As String)
  Declare Operator Cast() As String
End Type

Constructor Record()
End Constructor

Constructor Record(account As String, password As String, uid As Integer, gid As Integer, user As Person, _
directory As String, shell As String)

  With This
   .account   = account
   .password  = password
   .uid       = uid
   .gid       = gid
   .user      = user
   .directory = directory
   .shell     = shell
  End With

End Constructor 

Operator Record.Cast() As String
  Return account + ":" + password + ":" + Str(uid) + ":" + Str(gid) + ":" + user + ":" + directory + ":" + shell
End Operator

Dim persons(1 To 3) As Person
persons(1) = Person("Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org")
persons(2) = Person("Jane Doe",  "Room 1004", "(234)555-8914", "(234)555-0044", "jdoe@rosettacode.org"  )
persons(3) = Person("X Yz",      "Room 1003", "(234)555-8913", "(234)555-0033", "xyz@rosettacode.org"   )

Dim records(1 To 3) As Record
records(1) = Record("jsmith", "x", 1001, 1000, persons(1), "/home/jsmith", "/bin/bash")
records(2) = Record("jdoe",   "x", 1002, 1000, persons(2), "/home/jdoe"  , "/bin/bash")	
records(3) = Record("xyz",    "x", 1003, 1000, persons(3), "/home/xyz"   , "/bin/bash")
   
Open "passwd.txt" For Output As #1
Print #1, records(1)
Print #1, records(2)
Close #1

Open "passwd.txt" For Append Lock Write As #1
Print #1, records(3)
Close #1

  

You may also check:How to resolve the algorithm FASTA format step by step in the TMG programming language
You may also check:How to resolve the algorithm Hello world/Newbie step by step in the Groovy programming language
You may also check:How to resolve the algorithm Attractive numbers step by step in the Ada programming language
You may also check:How to resolve the algorithm Cuban primes step by step in the Rust programming language
You may also check:How to resolve the algorithm Substring step by step in the C programming language