用到的是NSString中的initWithContentsOfFile: encoding方法

//
//  main.m
//  读取指定文件并输出内容
//
//  Created by Apple on 15/11/24.
//  Copyright © 2015年 Apple. All rights reserved.
//

/*
 *读取指定txt文件,并把文件中的内容输出出来,
 */
#import <Foundation/Foundation.h>

int main(int argc, const char * argv[]) {

    NSError *error = nil;
    NSMutableString *path = [NSMutableString stringWithCapacity:42];
    NSString *home = [@"~" stringByExpandingTildeInPath];
    [path appendString:home];
    [path appendString:@"/work/temp.txt"];
    //NSString *string = [[NSString alloc] initWithContentsOfFile:@"/Users/apple/work/temp.txt" encoding:NSUTF8StringEncoding error:&error];
    NSString *string = [[NSString alloc] initWithContentsOfFile:path encoding:NSUTF8StringEncoding error:&error];

    //如果有报错,则把报错信息输出来
    if (error != nil) {
        NSLog(@"%@",[error localizedDescription]);
    }

    NSLog(@"%@",string);

    return 0;
}
View Code

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-07-22
猜你喜欢
  • 2022-02-09
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2022-12-23
  • 2021-11-12
  • 2022-02-10
相关资源
相似解决方案