[object-c 2.0 程序设计]object-c file handle (二)

//
//  main.m
//  cmdTry
//
//  Created by Calos Chen on 2017/8/21.
//  Copyright © 2017年 Calos Chen. All rights reserved.
//

#import <Foundation/Foundation.h>
#import "class4.m"
#import "NSObject_class5.h"

@class Fraction;
@interface Fraction : NSObject
{
    int numberator;
    int denominator;
    NSString* name;
    @public
    double price;
    Fraction *myf;
    
}
@property double price;
-(void) print;
-(void) setNumberator: (int)n;
-(void) setName: (NSString*)na;
-(void) setInfo: (int) id name:(NSString*)name;


@end

@implementation Fraction

@synthesize price;

-(void) print
{
    name= [name getBy:23 andName:@"Ok"];//
    NSLog(@"%i it is %@",numberator,name);
}
-(void) setNumberator:(int)n
{
    price=5;
    numberator=n;
}
-(void)setName:(NSString *)na{
    name=na;
}

-(void) setInfo:(int)id name:(NSString *)na{
    id=id;
    name=na;
}

@end

//---- program section ----

int main(int argc, const char * argv[]) {
    @autoreleasepool {
        // insert code here...
        NSLog(@"Hello, World!");
        NSLog(@"who are you!");
    }
    NSNumber *xdd;
    xdd=[NSNumber numberWithInt:45];
    
    NSLog(@"why is it");
    Fraction *myf= [[Fraction alloc] init];
    [myf setName:@"calos"];
    [myf print];
    double price=myf->price;
    
    int a1=5;
    float a2=3.23;
    double a3=3.44;
    char a4='c';
    int a5=(int)a2;
    for (int b=1; b<10;b++ ) {
        a5+=b;
    }
    myf.price=88;
    double b1=myf.price;
    BOOL isPrime=NO;
    NSLog(@"%i %f %e %c %i %c %g %e",a1,a2,a3,a4,a5,isPrime,b1,price);
    NSLog(@"end........");
  
    //file write
    NSFileManager* fm=[NSFileManager defaultManager];
    NSString* fname=@"a.txt";
    NSFileHandle* fh=[NSFileHandle new];
    NSData* data=nil;
    NSCoder* coder=[NSCoder new];
    [fm createFileAtPath:fname contents:nil attributes:nil];
    if([fm isReadableFileAtPath:fname]){
        [fh writeData:data];
    }
    else{
        [[fh initWithCoder:coder] writeData:data];
    }
    
    // 初始化管理类
    NSFileManager * manager = [NSFileManager
                               defaultManager];
    
    // 路径
    NSString * DirectoryPath = [NSHomeDirectory()
                                stringByAppendingPathComponent:@"/desktop/我的文件夹1/我的文件夹2"];
    
    NSError * error =
    nil;
    if ([manager
         createDirectoryAtPath:DirectoryPath
         withIntermediateDirectories:NO
         attributes:nil
         error:&error] !=
        YES) {
        //            NSString * str = [error localizedDescription];
        NSLog(@"创建失败");
    }else {
        //            NSString * str = [error localizedDescription];
        NSLog(@"创建成功");
    }

    
    //file handle

    NSFileHandle *inFile, *outFile;
    NSData *buffer;
    NSData* d1=[@"sss" dataUsingEncoding:NSUTF8StringEncoding];
    [[NSFileManager defaultManager] createFileAtPath:@"test.txt" contents:d1 attributes:nil];
    
    //打开testfile.txt文件用于读取操作
    inFile = [NSFileHandle fileHandleForReadingAtPath:@"testfile.txt"];
    
    if(inFile == nil)
    {
        NSLog(@"Open of testfile.txt for reading failed!");
        return 1;
    }
    
    //创建一个文件用于写数据(第一次是必要的)
    [[NSFileManager defaultManager] createFileAtPath:@"testout.txt" contents:nil attributes:nil] ;
    
    //打开testout.txt文件用于写入操作
    outFile = [NSFileHandle fileHandleForWritingAtPath:@"testout.txt"];
    
    if(outFile == nil)
    {
        NSLog(@"Open of testout.txt for writing failed!");
        return 2;
    }
    
    //
    [outFile truncateFileAtOffset:0];
    
    //从inFile中读取数据,并将其写入到outFile中
    buffer = [inFile readDataToEndOfFile];
    
    [outFile writeData:buffer];
    
    //关闭两个文件
    [inFile closeFile];
    [outFile closeFile];
    
    return 0;
}

//for today: 2017-08-21, I have read and practiced the first 153 pages of the object-c 2.0, now, I have known object-c classes and data types, and how to announce and implement the classes and class members, as well as functions in the class implementation. also property synthesize methods. loops.
//homework: implement a new class, and new interface and fill the class with members and all tangible members, then use all of them. @class declaration. So, write a console program with object-c.

 目前可以使用 Object-c 的 file 处理, 可以操作目录,并读写文件, 控制台程序生成了正常的文件,感觉比较欣慰,文件处理也算蛮大的一步了,知道了一些 oc 常用的库处理,在渐渐地变熟练

posted @ 2017-08-23 21:55  calochCN  阅读(184)  评论(1编辑  收藏  举报