Files
SwiftShell/SwiftShellTests/Scripts/readandwritefiles.swift
T
Kare Morstol e74189d008 Make tempdirectory a String instead of a NSURL.
The fact that NSURLs cannot be created with string literals makes them too cumbersome to use.
2014-12-02 00:17:39 +01:00

33 lines
811 B
Swift
Executable File

#!/usr/bin/env swiftshell
import SwiftShell
let filepath = tempdirectory + "/newfile.txt"
// File doesn't exist. Create it.
let file1 = open(forWriting: filepath )
file1.writeln("line 1")
file1.closeStream()
// File does exist. Append it.
let file2 = open(forWriting: filepath)
file2.writeln("line 2")
file2.closeStream()
// Test file.
let file3 = open(filepath)
if file3.read() != "line 1\nline 2\n" {
printErrorAndExit( "newfile.txt should contain 'line 1\\n line 2\\n'." )
}
// File exists. Overwrite it.
let file4 = open(forWriting: filepath, overwrite: true)
file4.write("file now contains only this")
file4.closeStream()
// Test file.
let file5 = open(filepath)
if file5.read() != "file now contains only this" {
printErrorAndExit( "newfile.txt should contain 'file now contains only this'." )
}