Append a record to the end of a text file: Difference between revisions

(Initial FutureBasic task solution added)
imported>Regattaguru
 
(One intermediate revision by one other user not shown)
Line 4,436:
 
Note that flock uses advisory lock; some other program (if it doesn't use flock) can still unexpectedly write to the file.
=={{header|Swift}}==
<syntaxhighlight lang="swift">
import Foundation
 
func appendPasswd(
account: String,
passwd: String,
uid: Int,
gid: Int,
bio: String,
home: String,
shell: String
) {
let str = [
account,
passwd,
String(uid),
String(gid),
bio,
home,
shell
].joined(separator: ":").appending("\n")
guard let data = str.data(using: .utf8) else { return }
let url = URL(fileURLWithPath: "./passwd")
do {
if let fileHandle = try? FileHandle(forWritingTo: url) {
fileHandle.seekToEndOfFile()
fileHandle.write(data)
try? fileHandle.close()
} else {
try data.write(to: url)
}
} catch {
print(error)
}
}
</syntaxhighlight>
Usage:
<syntaxhighlight lang="swift">
appendPasswd(
account: "jsmith",
passwd:"x",
uid: 1001,
gid: 1000,
bio: "Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org",
home: "/home/jsmith",
shell: "/bin/bash"
)
</syntaxhighlight>
 
=={{header|Tcl}}==
Line 4,710 ⟶ 4,759:
 
To append a record to an existing file in a CLI script, a little digging is required as the method to do this (''File.openWithFlags'') is currently undocumented. However, the following works fine.
<syntaxhighlight lang="ecmascriptwren">import "io" for File, FileFlags
 
var records = [
Anonymous user