1. 程式人生 > >[object-c 2.0 程序設計]object-c file handle (二)

[object-c 2.0 程序設計]object-c file handle (二)

attribute 正常的 比較 eat aps const component pub home

//
//  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* [email protected]"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 常用的庫處理,在漸漸地變熟練

[object-c 2.0 程序設計]object-c file handle (二)