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

Source code in the futurebasic programming language

local fn AppendRecordToFile( pwStr as CFStringRef, url as CFURLRef ) as BOOL
  ErrorRef     err = NULL
  BOOL     success = YES
  CFDataRef pwData = fn StringData( pwStr, NSUTF8StringEncoding )
  FileHandleRef fh = fn FileHandleForWritingToURL( url, @err )
  if ( err )
    success = fn FileManagerFileExistsAtURL( url )
    if success == NO then fn FileManagerCreateFileAtURL( url, pwData, NULL ) : success = YES : exit fn
  end if
  success = fn FileHandleSeekToEnd( fh, NULL, @err )
  if success == NO then print fn ErrorLocalizedDescription( err ) : exit fn
  success = fn FileHandleWriteData( fh, pwData, @err )
  if success == NO then print fn ErrorLocalizedDescription( err ) : exit fn
  success = fn FileHandleClose( fh, @err )
  if success == NO then print fn ErrorLocalizedDescription( err ) : exit fn
end fn = success

local fn ReadFileRecords( url as CFURLRef )
  ErrorRef err = NULL
  
  CFDataRef pwData = fn DataWithContentsOfURL( url, NSDataReadingMappedIfSafe, @err )
  if err then print fn ErrorLocalizedDescription( err ) : exit fn
  print fn StringWithData( pwData, NSUTF8StringEncoding )
end fn

local fn ProcessRecords
  BOOL          success = NO
  CFURLRef   desktopURL = fn FileManagerURLForDirectory( NSDesktopDirectory, NSUserDomainMask )
  CFURLRef          url = fn URLByAppendingPathComponent( desktopURL, @"passwords.txt" )
  CFArrayRef  pwRecords = @[@"account:password:UID:GID:fullname,office,extension,homephone,email:directory:shell\n",¬
  @"jsmith:x:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash\n",¬
  @"jdoe:x:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash\n"]
  NSUInteger   i, count = len(pwRecords)
  
  for i = 0 to count - 1
    success = fn AppendRecordToFile( pwRecords[i], url )
    if success then printf @"Record appended to file." else printf @"Failed to append record."
  next
  
  fn ReadFileRecords( url )
end fn

fn ProcessRecords

HandleEvents

  

You may also check:How to resolve the algorithm Ackermann function step by step in the Factor programming language
You may also check:How to resolve the algorithm 2048 step by step in the Racket programming language
You may also check:How to resolve the algorithm Anadromes step by step in the F# programming language
You may also check:How to resolve the algorithm Ramanujan's constant step by step in the C++ programming language
You may also check:How to resolve the algorithm Arithmetic evaluation step by step in the EasyLang programming language