Secure temporary file: Difference between revisions

m
m (syntax highlighting fixup automation)
m (→‎{{header|Wren}}: Minor tidy)
 
(11 intermediate revisions by 7 users not shown)
Line 29:
end Temp_File;</syntaxhighlight>
 
=={{header|BBC BASIC}}==
==={{header|BBC BASIC}}===
{{works with|BBC BASIC for Windows}}
The file is automatically deleted when closed.
Line 63 ⟶ 64:
Hello world!
</pre>
 
==={{header|FreeBASIC}}===
{{trans|BBC BASIC}}
The file is deleted when closed.
<syntaxhighlight lang="vb">Dim As Long f
Dim As String message
 
f = Freefile
Open "temp.txt" For Output As #f
If Err > 0 Then Print "Failed to open temp"; f : End
Print #f, "Hello world!"
Close #f
 
Open "temp.txt" For Input As #f
Line Input #f, message
Close #f
Print message
 
Shell "del temp.txt"
 
Sleep</syntaxhighlight>
 
=={{header|C}}==
Line 99 ⟶ 121:
using System.IO;
 
Console.WriteLine(Path.GetTempFileName());</syntaxhighlight>=={{header|C sharp|C#}}==
 
=={{header|C++}}==
<syntaxhighlight lang="c++">
#include <cstdio>
 
int main() {
// Creates and opens a temporary file with a unique auto-generated filename.
// If the program closes the file, the file is automatically deleted.
// The file is also automatically deleted if the program exits normally.
std::FILE* temp_file_pointer = std::tmpfile();
 
// Using functions which take a file pointer as an argument
std::fputs("Hello world", temp_file_pointer);
std::rewind(temp_file_pointer);
char buffer[12];
std::fgets(buffer, sizeof buffer, temp_file_pointer);
printf(buffer);
}
</syntaxhighlight>
{{ out }}
<pre>
Hello world
</pre>
 
=={{header|Clojure}}==
Line 158 ⟶ 203:
<syntaxhighlight lang="lisp">(make-temp-file "prefix")
;; => "/tmp/prefix25452LPe"</syntaxhighlight>
 
=={{header|F_Sharp|F#}}==
<syntaxhighlight lang="fsharp">
printfn $"%s{System.IO.Path.GetTempFileName()}"
</syntaxhighlight>
{{out}}
<pre>
/tmp/tmpEuSgiY.tmp
</pre>
For the cynical who may not believe the file has been created
</pre>
nigel@nigel:/tmp$ ls *.tmp
tmpEuSgiY.tmp
</pre>
 
 
=={{header|Fortran}}==
Line 263 ⟶ 323:
}
}</syntaxhighlight>
 
Alternative example
<syntaxhighlight lang="java">
import java.io.BufferedWriter;
import java.nio.file.Files;
import java.io.IOException;
import java.nio.file.Path;
import java.nio.file.StandardOpenOption;
 
public final class SecureTemporaryFile {
 
public static void main(String[] args) throws IOException {
// Create a temporary file in the directory D:\.
// We should use java.nio.file.Files instead of the old java.io.File, as it is more secure.
// If the file cannot be created, it will throw an exception.
Path temporaryFilePath = Files.createTempFile(Path.of("D:/"), "example", ".tmp");
 
// For uniqueness, the Java API will insert a random number between the given prefix
// and the file extension.
System.out.println("Temporary file created: " + temporaryFilePath);
 
// Opening it with the following option will cause the file to be deleted when it is closed.
BufferedWriter tempFileWriter = Files.newBufferedWriter(
temporaryFilePath, StandardOpenOption.DELETE_ON_CLOSE);
// ... write to file, read it back in, close it...
}
 
}
</syntaxhighlight>
{{ out }}
<pre>
Temporary file created: D:\example12312088502442779987.tmp
</pre>
 
=={{header|Julia}}==
Line 302 ⟶ 395:
 
=={{header|Kotlin}}==
<syntaxhighlight lang="scalakotlin">//import version 1kotlin.io.1path.2createTempFile
import kotlin.io.path.deleteExisting
 
fun main() {
import java.io.File
val tempFilePath = createTempFile("example", ".tmp")
 
println("Temporary file created: $tempFilePath")
fun main(args: Array<String>) {
tempFilePath.deleteExisting()
try {
val tf = File.createTempFile("temp", ".tmp")
println(tf.absolutePath)
tf.delete()
}
catch (ex: Exception) {
println(ex.message)
}
}</syntaxhighlight>
Sample output (Ubuntu v14.04):
{{out}}
<pre>
Temporary file created: /tmp/example14437465325231438926.tmp
/tmp/temp1551492276377305257.tmp
</pre>
 
Line 436 ⟶ 523:
 
=={{header|OCaml}}==
From the module Filename, one can use the functions [httphttps://caml.inriaocaml.frorg/pub/docs/manual-ocaml/librefapi/Filename.html#VALtemp_file temp_file] or [httphttps://camlocaml.inria.fr/pub/docs/manual-ocamlorg/librefapi/Filename.html#VALopen_temp_file open_temp_file]
<syntaxhighlight lang="ocaml"># Filename.temp_file "prefix." ".suffix" ;;
- : string = "/home/blue_prawn/tmp/prefix.301f82.suffix"</syntaxhighlight>
Line 675 ⟶ 762:
 
=={{header|Ruby}}==
<syntaxhighlight lang="ruby">irb(main):001:0> require 'tempfile'
 
=> true
irb(main):002:0> f = Tempfile.new('foo')
f.path # => #<File:"/tmp/foo20081226-307-10p746n-0>"
f.close
irb(main):003:0> f.path
f.unlink # => #<Tempfile: (closed)></syntaxhighlight>
=> "/tmp/foo20081226-307-10p746n-0"
irb(main):004:0> f.close
=> nil
irb(main):005:0> f.unlink
=> #<Tempfile: (closed)></syntaxhighlight>
 
=={{header|Rust}}==
Line 728 ⟶ 811:
fh.print("Hello, World!\n");
fh.close;</syntaxhighlight>
 
=={{header|SparForte}}==
As a structured script.
<syntaxhighlight lang="ada">#!/usr/local/bin/spar
pragma annotate( summary, "tmpfile" );
pragma annotate( description, "Create a temporary file, securely and exclusively" );
pragma annotate( description, "(opening it such that there are no possible race" );
pragma annotate( description, "conditions). It's fine assuming local filesystem" );
pragma annotate( description, "semantics (NFS or other networking filesystems can" );
pragma annotate( description, "have signficantly more complicated semantics for" );
pragma annotate( description, "satisfying the 'no race conditions' criteria). The" );
pragma annotate( description, "function should automatically resolve name collisions" );
pragma annotate( description, "and should only fail in cases where permission is" );
pragma annotate( description, "denied, the filesystem is read-only or full, or similar" );
pragma annotate( description, "conditions exist (returning an error or raising an" );
pragma annotate( description, "exception as appropriate to the language/environment)." );
pragma annotate( see_also, "http://rosettacode.org/wiki/Secure_temporary_file" );
pragma annotate( author, "Ken O. Burtch" );
pragma license( unrestricted );
 
pragma restriction( no_external_commands );
 
procedure tmpfile is
temp : file_type;
contents : string;
begin
? "Creating a temporary file";
create( temp );
put_line( temp, "Hello World");
 
? "Reading a temporary file";
reset( temp, in_file);
contents := get_line( temp );
put_line( "File contains: " & contents );
 
? "Discarding a temporary file";
close( temp );
end tmpfile;</syntaxhighlight>
 
=={{header|Standard ML}}==
Line 778 ⟶ 899:
{{libheader|Wren-ioutil}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "random" for Random
import "./ioutil" for File, FileUtil
import "./fmt" for Fmt
 
var rand = Random.new()
9,485

edits