代码之家  ›  专栏  ›  技术社区  ›  zekel lepture

在cocoa中,如何将文件的行尾更改为lf?

  •  0
  • zekel lepture  · 技术社区  · 15 年前

    我需要读取文件并手动迭代吗?我想能在LF和CRLF之间切换。

    2 回复  |  直到 15 年前
        1
  •  1
  •   Wevah    15 年前

    我相信有更多的内存利用方法,但这可能会为您提供:

    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 显然是文件的路径;替换 initWithContentsOfURL:... / writeToURL:... 如果你喜欢的话。

        2
  •  1
  •   Matthieu Cormier Adam Musch    15 年前

    您可以在终端中使用“tr”命令。

    推荐文章