Append a record to the end of a text file: Difference between revisions
Content added Content deleted
(→{{header|Elixir}}: improvement) |
|||
Line 1,661: | Line 1,661: | ||
|} |
|} |
||
Note that flock uses advisory lock; some other program (if it doesn't use flock) can still unexpectedly write to the file. |
Note that flock uses advisory lock; some other program (if it doesn't use flock) can still unexpectedly write to the file. |
||
=={{header|Phix}}== |
|||
{|class="wikitable" style="text-align: center; margin: 1em auto 1em auto;" |
|||
|+ Append Capabilities. |
|||
|- |
|||
!colspan=2| Data Representation |
|||
!rowspan=2| IO<BR>Library |
|||
!rowspan=2| Append<BR>Possible |
|||
!rowspan=2| Automatic<BR>Append |
|||
!rowspan=2| Multi-tasking<BR>Safe |
|||
|- |
|||
! In core || On disk |
|||
|- |
|||
| sequence || text file || builtin || ☑ || ☑ || ☑ (Advisory lock) |
|||
|} |
|||
Locking is used to ensure multitasking safety. Note that on Phix, "multitasking" is the kid brother of multithreading, and the |
|||
calls to task_yield() in the code that follows are there to respect the phix-specific definition of multiple tasks being in the |
|||
same process, in which case locking does not make any real difference - because there is no task_yield() in a locked state. |
|||
You can also test the locking when running multiple processes/threads by uncommenting the wait_key() lines. |
|||
<lang Phix>constant filename = "passwd.txt" |
|||
integer fn |
|||
constant rec1 = {"jsmith","x",1001,1000,{"Joe Smith","Room 1007","(234)555-8917","(234)555-0077","jsmith@rosettacode.org"},"/home/jsmith","/bin/bash"}, |
|||
rec2 = {"jdoe","x",1002,1000,{"Jane Doe","Room 1004","(234)555-8914","(234)555-0044","jdoe@rosettacode.org"},"/home/jdoe","/bin/bash"}, |
|||
rec3 = {"xyz","x",1003,1000,{"X Yz","Room 1003","(234)555-8913","(234)555-0033","xyz@rosettacode.org"},"/home/xyz","/bin/bash"} |
|||
function tostring(sequence record) |
|||
record[3] = sprintf("%d",{record[3]}) |
|||
record[4] = sprintf("%d",{record[4]}) |
|||
record[5] = join(record[5],",") |
|||
record = join(record,":") |
|||
return record |
|||
end function |
|||
procedure wait(string what) |
|||
?sprintf("wait (%s)",{what}) |
|||
sleep(1) |
|||
task_yield() |
|||
end procedure |
|||
if not file_exists(filename) then |
|||
fn = open(filename,"w") |
|||
if fn!=-1 then -- (someone else just beat us to it?) |
|||
printf(fn,"account:password:UID:GID:fullname,office,extension,homephone,email:directory:shell\n") |
|||
printf(fn,"%s\n",{tostring(rec1)}) |
|||
printf(fn,"%s\n",{tostring(rec2)}) |
|||
close(fn) |
|||
end if |
|||
end if |
|||
while 1 do |
|||
fn = open(filename,"a") |
|||
if fn!=-1 then exit end if |
|||
wait("append") |
|||
end while |
|||
-- ?"file open in append mode"; {} = wait_key() |
|||
while 1 do |
|||
if lock_file(fn,LOCK_EXCLUSIVE,{}) then exit end if |
|||
wait("lock") |
|||
end while |
|||
-- ?"file locked"; {} = wait_key() |
|||
printf(fn,"%s\n",{tostring(rec3)}) |
|||
unlock_file(fn,{}) |
|||
close(fn) |
|||
while 1 do |
|||
fn = open(filename,"r") |
|||
if fn!=-1 then exit end if |
|||
wait("read") |
|||
end while |
|||
?gets(fn) |
|||
while 1 do |
|||
object line = gets(fn) |
|||
if atom(line) then exit end if |
|||
?line |
|||
{line} = scanf(line,"%s:%s:%d:%d:%s:%s:%s\n") |
|||
line[5] = split(line[5],',') |
|||
?line |
|||
end while |
|||
close(fn)</lang> |
|||
{{out}} |
|||
<pre> |
|||
"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" |
|||
{"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\n" |
|||
{"jdoe","x",1002,1000,{"Jane Doe","Room 1004","(234)555-8914","(234)555-0044","jdoe@rosettacode.org"},"/home/jdoe","/bin/bash"} |
|||
"xyz:x:1003:1000:X Yz,Room 1003,(234)555-8913,(234)555-0033,xyz@rosettacode.org:/home/xyz:/bin/bash\n" |
|||
{"xyz","x",1003,1000,{"X Yz","Room 1003","(234)555-8913","(234)555-0033","xyz@rosettacode.org"},"/home/xyz","/bin/bash"} |
|||
</pre> |
|||
=={{header|PicoLisp}}== |
=={{header|PicoLisp}}== |
||
Line 1,690: | Line 1,779: | ||
(bye)</lang> |
(bye)</lang> |
||
=={{header|PowerShell}}== |
=={{header|PowerShell}}== |
||
I treated the file as a CSV file without header information. File testing is minimal. |
I treated the file as a CSV file without header information. File testing is minimal. |