Secure temporary file: Difference between revisions

m
(Secure temporary file in FreeBASIC)
m (→‎{{header|Wren}}: Minor tidy)
 
(7 intermediate revisions by 3 users not shown)
Line 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 180 ⟶ 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 285 ⟶ 323:
}
}</syntaxhighlight>
 
Alternative example
<syntaxhighlight lang="java">
import java.io.FileBufferedWriter;
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.
valPath tftemporaryFilePath = FileFiles.createTempFile(Path.of("D:/"), "tempexample", ".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 324 ⟶ 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 834 ⟶ 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,476

edits