Pure Programmer
Blue Matrix


Cluster Map

Stream I/O

L1

This page is under construction. Please come back later.

StreamIO1.swift
#!/usr/bin/env swift;
/******************************************************************************
 * This program simply copies an input stream to the console character by
 * character like the Unix 'cat' program.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

import Foundation
import Utils

// Begin Main
var c:Int?
c = Utils.getchar()
while c != nil {
	print(Utils.chr(c!), terminator: "")
	c = Utils.getchar()
}

exit(EXIT_SUCCESS)

Output
$ swiftc StreamIO1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc StreamIO1.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
StreamIO2.swift
#!/usr/bin/env swift;
/******************************************************************************
 * This program simply copies an input stream to the console line by line.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

import Foundation

// Begin Main
var line:String?
line = (readLine(strippingNewline: true))
while line != nil {
	print(line!)
	line = (readLine(strippingNewline: true))
}

exit(EXIT_SUCCESS)

Output
$ swiftc StreamIO2.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc StreamIO2.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
StreamIO3.swift
#!/usr/bin/env swift;
/******************************************************************************
 * This program reads bytes from STDIN and prints them in decimal format.
 * 
 * Copyright © 2020 Richard Lesh.  All rights reserved.
 *****************************************************************************/

import Foundation
import Utils

// Begin Main
var c:UInt8?
var count:Int = 0
c = Utils.getbyte(FileHandle.standardInput)
while c != nil {
	print(String(c!) + " ", terminator: "")
	count += 1
	if count % 20 == 0 {
		print()
	}
	c = Utils.getbyte(FileHandle.standardInput)
}
if count % 20 != 0 {
	print()
}

exit(EXIT_SUCCESS)

Output
$ swiftc StreamIO3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation) $ swiftc StreamIO3.swift -I . -L . -lUtils error: link command failed with exit code 1 (use -v to see invocation) ld: library not found for -lUtils clang: error: linker command failed with exit code 1 (use -v to see invocation)
swift

Questions

Projects

More ★'s indicate higher difficulty level.

References