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

m
no edit summary
mNo edit summary
Line 1,562:
{{out}}
<pre>Appended Record: xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash</pre>
 
=={{header|Julia}}==
<lang julia>
using SHA # security instincts say do not write bare passwords to a shared file even in toy code :)
 
mutable struct Personnel
fullname::String
office::String
extension::String
homephone::String
email::String
Personnel(ful,off,ext,hom,ema) = new(ful,off,ext,hom,ema)
end
 
mutable struct Passwd
account::String
password::String
uid::Int32
gid::Int32
personal::Personnel
directory::String
shell::String
Passwd(acc,pas,uid,gid,per,dir,she) = new(acc,pas,uid,gid,per,dir,she)
end
 
function writepasswd(filename, passrecords)
if(passrecords isa Array) == false
passrecords = [passrecords]
end
fh = open(filename, "a")
for pas in passrecords
record = join([pas.account, bytes2hex(sha256(pas.password)), pas.uid, pas.gid,
join([pas.personal.fullname, pas.personal.office, pas.personal.extension,
pas.personal.homephone, pas.personal.email], ','),
pas.directory, pas.shell], ':')
write(fh, record, "\n")
end
close(fh)
end
 
const jsmith = Passwd("jsmith","x",1001, 1000, Personnel("Joe Smith", "Room 1007", "(234)555-8917", "(234)555-0077", "jsmith@rosettacode.org"), "/home/jsmith", "/bin/bash")
const jdoe = Passwd("jdoe","x",1002, 1000, Personnel("Jane Doe", "Room 1004", "(234)555-8914", "(234)555-0044", "jdoe@rosettacode.org"), "/home/jdoe", "/bin/bash")
const xyz = Passwd("xyz","x",1003, 1000, Personnel("X Yz", "Room 1003", "(234)555-8913", "(234)555-0033", "xyz@rosettacode.org"), "/home/xyz", "/bin/bash")
 
const pfile = "pfile.csv"
writepasswd(pfile, [jsmith, jdoe])
println("Before last record added, file is:\n$(readstring(pfile))")
writepasswd(pfile, xyz)
println("After last record added, file is:\n$(readstring(pfile))")
</lang>
{{output}}<pre>
Before last record added, file is:
jsmith:2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash
jdoe:2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash
 
After last record added, file is:
jsmith:2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881:1001:1000:Joe Smith,Room 1007,(234)555-8917,(234)555-0077,jsmith@rosettacode.org:/home/jsmith:/bin/bash
jdoe:2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881:1002:1000:Jane Doe,Room 1004,(234)555-8914,(234)555-0044,jdoe@rosettacode.org:/home/jdoe:/bin/bash
xyz:2d711642b726b04401627ca9fbac32f5c8530fb1903cc4db02258717921a4881:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash
</pre>
 
=={{header|Mathematica}} / {{header|Wolfram Language}}==
4,103

edits