1

I have a function that reads line by line from a file and saved it into a variable line. I am trying to make it so that it reads a line and writes it to another file. Reads the next line and adds that line to the file. I have this code:

if let readFileObj = ReadWriteManager(path: readFilePath!) {
        while let line = readFileObj.nextLine() {
            println(line)
        line.writeToFile(NSHomeDirectory().stringByAppendingPathComponent("Documents").stringByAppendingPathComponent("textfile.txt"), atomically: false, encoding: NSUTF8StringEncoding, error: nil)
        }
}

I have a class ReadWriteManager that takes the path of the file to be read. That class has a function nextLine() that returns the next line of the file as a string. When I do println, my console output screen shows all of the file content. Unfortunate when I try to write it to a file textfile.txt using .writeToFile(), it keeps rewriting the same line over and over again. So when I check to see if the files successfully copied, it only shows the last line of the file that was read. How can I make it so that it'll write a line then write to the next line without rewriting the same line over and over again? I would greatly appreciate the help! Been stumbled on this for a while :(

This is my file to be read:

Sample
Text With
Multiple
Lines
}

My output console shows this:

Sample
Text With
Multiple
Lines
}

But my file that it was writing to shows only the last line which is:

}

EDIT: This is the solution

let writeFileHandle = NSFileHandle(forWritingAtPath: myWriteFile) // Declare the write file handler
while var lineRead = readFileObj?.nextLine() { // Read line by line from the template
        lineRead += "\n"
        let data = (lineRead as NSString).dataUsingEncoding(NSUTF8StringEncoding) // Convert the String to NSData object
        writeFileHandle?.writeData(data!) // Write the NSData object to the file
}
MB41
  • 552
  • 2
  • 8
  • 24
  • You need to post the rest of the code – Leo Dabus Dec 05 '14 at 06:04
  • @LeonardoSavioDabus I added some more information. I hope this helps more – MB41 Dec 05 '14 at 06:29
  • @Billy: I am just curious: where does ReadWriteManager come from? – Martin R Dec 05 '14 at 08:12
  • @MartinR ReadWriteManager is a class i made that takes the path of the file it is going to read. I created `readFileObj` object so that I can access the classes's functions. Using the path of the file it is going to read, it takes line by line and writes it to a different file (I created functions in that class to do this). – MB41 Dec 05 '14 at 09:16
  • @Billy: So you might be interested in this: http://stackoverflow.com/a/24648951/1187415 (for the reading part only). The API is actually identical to yours. – Martin R Dec 05 '14 at 09:22

1 Answers1

2

The method -[NSString writeToFile:atomically:encoding:error:] writes out a file containing the string. The string constitutes the whole contents of the new file. That's what it does.

You need to use a completely different approach. For example, you can create an NSFileHandle using +fileHandleForWritingAtPath: before your loop and then repeatedly write to it using -writeData:. You would need to convert the string to a data object using -[NSString dataUsingEncoding:]. If your strings don't already have newlines, you'll want to append them before converting to data objects.

Ken Thomases
  • 88,520
  • 7
  • 116
  • 154
  • I changed my code to this: `let readDataFile = NSFileHandle(forWritingAtPath: myWriteFile) while let line = myReadFile.nextLine() { let data = (line as NSString).dataUsingEncoding(NSUTF8StringEncoding) data?.writeToFile(myWriteFile, atomically: true) }`Sadly it's no help :( – MB41 Dec 05 '14 at 07:09
  • That's not what I suggested. Just like the similar method on `NSString`, `-[NSData writeToFile:atomically:]` writes a new file containing just that data. You need to write to the `NSFileHandle` using something like `readDataFile.writeData(data)`. (By the way, why is it called `readDataFile`? Shouldn't that be `writeDataFile`?) – Ken Thomases Dec 05 '14 at 07:46
  • I got it to work! `let writeFileHandle = NSFileHandle(forWritingAtPath: myWriteFile) while var lineRead = myReadFile.nextLine() { lineRead += "\n" let data = (lineRead as NSString).dataUsingEncoding(NSUTF8StringEncoding) writeFileHandle?.writeData(data!) }` – MB41 Dec 05 '14 at 07:55
  • You ended up doing what I told you to do: adding "\n" otherwise it wouldn't work and you unchecked the answer, thats great. The question was about the lines on top of each other. – Leo Dabus Dec 05 '14 at 18:52
  • I cannot upvote yet and I can't check two answers. Both were very good, this just made more sense for me because I am able to read line, modify it, then write it. Read next line, modify, then write and so on. – MB41 Dec 05 '14 at 20:14