I don't know what the text file encoding is. I know that there are lots of "\0" that shouldn't be there.
related question: is there any way, in swift or elsewhere to remove all non-ASCII characters from a text file?
I read the file with this function I got off the net:
func nextLine() -> String? {
precondition(fileHandle != nil, "Attempt to read from closed file")
if atEof {
return nil
}
/
var range = buffer.rangeOfData(delimData, options: [], range: NSMakeRange(0, buffer.length))
while range.location == NSNotFound {
let tmpData = fileHandle.readDataOfLength(chunkSize)
if tmpData.length == 0 {
/
atEof = true
if buffer.length > 0 {
/
let line = NSString(data: buffer, encoding: encoding)
buffer.length = 0
return line as String?
}
/
return nil
}
buffer.appendData(tmpData)
range = buffer.rangeOfData(delimData, options: [], range: NSMakeRange(0, buffer.length))
}
/
let line = NSString(data: buffer.subdataWithRange(NSMakeRange(0, range.location)),
encoding: encoding)
/
buffer.replaceBytesInRange(NSMakeRange(0, range.location + range.length), withBytes: nil, length: 0)
return line as String?
}
/
func rewind() -> Void {
fileHandle.seekToFileOffset(0)
buffer.length = 0
atEof = false
}
/
func close() -> Void {
fileHandle?.closeFile()
fileHandle = nil
}
}
extension StreamReader : SequenceType {
func generate() -> AnyGenerator<String> {
return anyGenerator {
return self.nextLine()
}
}
}
rhia ia the code that calls Streamreader:
let directory = get_string_directory_for_data_files()
fullpath = String(directory) + "/Accounting/new accnt transactions"
print("the path is \(fullpath)")
da_reader = StreamReader(path: fullpath)!
last_absolute_rec_number = 0
for line in da_reader {
thanks for any help!