I've been having trouble figuring out how to read and write data to a textfile, and not finding much out there for Swift on Linux. I found code for reading data from a file on StackOverflow that works for me, but cludged together my own code for writing to a file. I'm not sure exactly what I'm doing, but am making some progress. Can anyone point me toward a good source of help for reading and writing data in Swift on Linux. Thanks!
This is the code that I found that works for reading data: // Try to read from a file // 5/16/2016 import Glibc //import a Linux library let path = "Resources/sampleIn.txt" let BUFSIZE = 1024 print("attempting to open a file for input") let fp = fopen(path, "r") if fp == nil {print("error reading file")} if fp != nil { print("reading...") var buf = [CChar](count:BUFSIZE, repeatedValue:CChar(0)) while fgets(&buf, Int32(BUFSIZE), fp) != nil { print(String.fromCString(buf)!, terminator:"") } } This is the code I pieced together for writing data: import Glibc //import a Linux library let path = "Resources/sampleOut.txt"let BUFSIZE = 1024print("attempting to open a file for Output")let fp = fopen(path, "w+")if fp == nil {print("error writing file")}if fp != nil { print("Type a few words to be saved to a file") var fewWords=readLine()! fputs(fewWords,fp) print("writing...") } fclose(fp) Any help would be appreciated. Thanks! John -- John Myers Mathematics and Computer Science Teacher ------------------------------------------------------------------
_______________________________________________ swift-users mailing list swift-users@swift.org https://lists.swift.org/mailman/listinfo/swift-users