Read a text file line by line in Swift?

前端 未结 6 739
广开言路
广开言路 2020-12-05 04:14

I just started learning Swift. I have got my code to read from the text file, and the App displays the content of the entire text file. How can I display line by line and ca

6条回答
  •  孤街浪徒
    2020-12-05 04:31

    Swift 5.2

    The solution below shows how to read one line at a time. This is quite different from reading the entire contents into memory. Reading line-by-line scales well if you have a large file to read. Putting an entire file into memory does not scale well for large files.

    The example below uses a while loop that quits when there are no more lines, but you can choose a different number of lines to read if you wish.

    The code works as follows:

    1. create a URL that tells where the file is located
    2. make sure the file exists
    3. open the file for reading
    4. set up some initial variables for reading
    5. read each line using getLine()
    6. close the file when done

    You could make the code less verbose if you wish; I have included comments to explain what the variables' purposes are.

    Swift 5.2

    import Cocoa
    
    // get URL to the the documents directory in the sandbox
    let home = FileManager.default.homeDirectoryForCurrentUser
    
    // add a filename
    let fileUrl = home
        .appendingPathComponent("Documents")
        .appendingPathComponent("my_file")
        .appendingPathExtension("txt")
    
    
    // make sure the file exists
    guard FileManager.default.fileExists(atPath: fileUrl.path) else {
        preconditionFailure("file expected at \(fileUrl.absoluteString) is missing")
    }
    
    // open the file for reading
    // note: user should be prompted the first time to allow reading from this location
    guard let filePointer:UnsafeMutablePointer = fopen(fileUrl.path,"r") else {
        preconditionFailure("Could not open file at \(fileUrl.absoluteString)")
    }
    
    // a pointer to a null-terminated, UTF-8 encoded sequence of bytes
    var lineByteArrayPointer: UnsafeMutablePointer? = nil
    
    // the smallest multiple of 16 that will fit the byte array for this line
    var lineCap: Int = 0
    
    // initial iteration
    var bytesRead = getline(&lineByteArrayPointer, &lineCap, filePointer)
    
    defer {
        // remember to close the file when done
        fclose(filePointer)
    }
    
    while (bytesRead > 0) {
        
        // note: this translates the sequence of bytes to a string using UTF-8 interpretation
        let lineAsString = String.init(cString:lineByteArrayPointer!)
        
        // do whatever you need to do with this single line of text
        // for debugging, can print it
        print(lineAsString)
        
        // updates number of bytes read, for the next iteration
        bytesRead = getline(&lineByteArrayPointer, &lineCap, filePointer)
    }
    

提交回复
热议问题