Do I have to read the files and iterate manually? I'd like to be able to change between LF and CRLF.
No good way to do that in Cocoa without using NSTask to run a command?
zekel
2009-10-28 17:07:13
+1
A:
I'm sure there are more memory-efficient ways, but this might do the job for you:
NSStringEncoding usedEncoding;
NSMutableString *fileContents = [[NSMutableString alloc] initWithContentsOfFile:pathToFile usedEncoding:&usedEncoding error:nil];
// Normally you'd pass in an error and do the checking thing.
[fileContents replaceOccurrencesOfString:@"\n" withString:@"\r\n" options:NSLiteralSearch range:NSMakeRange(0, [fileContents length])];
// The other direction: [fileContents replaceOccurrencesOfString:@"\r\n" withString:@"\n" options:NSLiteralSearch range:NSMakeRange(0, [fileContents length])];
// Assumes you want to overwrite the file; again, normally you'd check for errors and such.
[fileContents writeToFile:filePath atomically:YES encoding:usedEncoding error:nil];
[fileContents release];
pathToFile
is obviously the path to the file; substitute the initWithContentsOfURL:...
/writeToURL:...
versions if you prefer.
Wevah
2009-10-28 18:02:21
I just realized that this will cause issues if you read in a file that already uses CRLF line-endings; hopefully you already know this ahead of time/are detecting this. >_<
Wevah
2009-10-28 18:03:55