从文件中读取数据再输出到文件!

文件格式:

101.093,32.3943
83.0319,41.8305
101.250,42.5169
108.993,32.7140
108.509,32.5173
109.038,32.6804
117.114,30.5775

 

//写数据

-(void)WriteToTxtTwo

{

    //泵车 "b" //green绿色

    NSMutableArray *rectMarray1= [selfgetRectDicWithFile:@"/Users/anyanyone/Desktop/bengche.txt"];  //bengche480   

    NSMutableDictionary* dict1 = [NSMutableDictionarydictionary];      

    int bj = 1;

    for (int i = 0; i < [rectMarray1 count]; i++)

    {          

        //if(i % 3 == 0)

        {

            NSArray* array = [rectMarray1 objectAtIndex:i];

            NSString* s = [NSString stringWithFormat:@"{%@,%@}",[array objectAtIndex:0],[array objectAtIndex:1]];

            [dict1 setObject:s forKey: [NSString stringWithFormat:@"%@%d",@"b", bj]];

            bj++;            

        }        

    }    

    [dict1 writeToFile:@"/Users/anyanyone/Desktop/bengche.txt" atomically:YES ];

}

 

//读取数据

-(NSMutableArray*)getRectDicWithFile:(NSString*)file

{

    NSString *filePath = file;    

    FILE *fp = fopen([filePath UTF8String], "r");

    NSMutableArray* array = [[NSMutableArrayalloc] init];

  //  NSMutableDictionary *_rectDic = [NSMutableDictionary dictionary];

    if(fp)

    {

        while(!feof(fp))

        {

            char  buf[BUFSIZ];

            fgets(buf,BUFSIZ,fp);

            NSString  *s=[[NSString alloc] initWithUTF8String:(const char *)buf];

            NSString *ss=[s stringByReplacingOccurrencesOfString:@"\r"withString:@""];

            ss = [ss stringByReplacingOccurrencesOfString:@"\n"withString:@""];

            //分割字符

            NSArray *a=[ss componentsSeparatedByString:@","];

            [s release];

         

            [array addObject:a];

        }

        fclose(fp);

    }

    return array;

}

posted @ 2012-09-07 14:02  六界剑仙  阅读(189)  评论(0编辑  收藏  举报