Secure temporary file: Difference between revisions

Content added Content deleted
(added php)
No edit summary
Line 6: Line 6:
This example creates a temporary file, writes to the file, then reads from the file.
This example creates a temporary file, writes to the file, then reads from the file.


<ada> with Ada.Text_Io; use Ada.Text_Io;
<lang ada> with Ada.Text_Io; use Ada.Text_Io;
procedure Temp_File is
procedure Temp_File is
Line 19: Line 19:
Get_Line(File => Temp, Item => Contents, Last => Length);
Get_Line(File => Temp, Item => Contents, Last => Length);
Put_Line(Contents(1..Length));
Put_Line(Contents(1..Length));
end Temp_File;</ada>
end Temp_File;</lang>


=={{header|C}}==
=={{header|C}}==
<c>#include <stdio.h>
<lang c>#include <stdio.h>


int main() {
int main() {
Line 35: Line 35:
return 0;
return 0;
}</c>
}</lang>


The following {{works with|POSIX}}
The following {{works with|POSIX}}
<c>#include <stdlib.h>
<lang c>#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>


Line 49: Line 49:
return 0;
return 0;
}</c>
}</lang>


=={{header|D}}==
=={{header|D}}==
{{works with|Tango}}
{{works with|Tango}}
<d>module tempfile ;
<lang d>module tempfile ;
import tango.io.TempFile, tango.io.Stdout ;
import tango.io.TempFile, tango.io.Stdout ;


Line 67: Line 67:


// both can only be accessed by the current user (the program?).
// both can only be accessed by the current user (the program?).
}</d>
}</lang>


=={{header|Haskell}}==
=={{header|Haskell}}==
Line 79: Line 79:


=={{header|Java}}==
=={{header|Java}}==
<java>import java.io.File;
<lang java>import java.io.File;


try {
try {
Line 91: Line 91:


} catch (IOException e) {
} catch (IOException e) {
}</java>
}</lang>


=={{header|OCaml}}==
=={{header|OCaml}}==
From the module Filename, one can use the functions [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Filename.html#VALtemp_file temp_file] or [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Filename.html#VALopen_temp_file open_temp_file]
From the module Filename, one can use the functions [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Filename.html#VALtemp_file temp_file] or [http://caml.inria.fr/pub/docs/manual-ocaml/libref/Filename.html#VALopen_temp_file open_temp_file]
<ocaml>
<lang ocaml>
# Filename.temp_file "prefix." ".suffix" ;;
# Filename.temp_file "prefix." ".suffix" ;;
- : string = "/home/blue_prawn/tmp/prefix.301f82.suffix"
- : string = "/home/blue_prawn/tmp/prefix.301f82.suffix"
</ocaml>
</lang>


=={{header|Perl}}==
=={{header|Perl}}==
function interface:
function interface:
<perl>use File::Temp qw(tempfile);
<lang perl>use File::Temp qw(tempfile);
$fh = tempfile();
$fh = tempfile();
($fh2, $filename) = tempfile(); # this file stays around by default
($fh2, $filename) = tempfile(); # this file stays around by default
print "$filename\n";
print "$filename\n";
close $fh;
close $fh;
close $fh2;</perl>
close $fh2;</lang>


object-oriented interface:
object-oriented interface:
<perl>use File::Temp;
<lang perl>use File::Temp;
$fh = new File::Temp;
$fh = new File::Temp;
print $fh->filename, "\n";
print $fh->filename, "\n";
close $fh;</perl>
close $fh;</lang>


=={{header|PHP}}==
=={{header|PHP}}==
<php>$fh = tmpfile();
<lang php>$fh = tmpfile();
// do stuff with $fh
// do stuff with $fh
fclose($fh);
fclose($fh);
Line 124: Line 124:
$filename = tempnam('/tmp', 'prefix');
$filename = tempnam('/tmp', 'prefix');
echo "$filename\n";
echo "$filename\n";
// open $filename and do stuff with it</php>
// open $filename and do stuff with it</lang>


=={{header|Python}}==
=={{header|Python}}==