iPhone:试图保存二进制文件

时间:2012-11-12 23:31:46

标签: iphone

我有一个数组定义为短FavList [300];

我尝试加载所有1,保存它。 然后在所有0中加载并加载它。

在读完数据之后,它仍然是全部的0.s,并没有加载所有的。

-(void)Load{

    // put in all 1's
    for(int i=0; i<AMOUNT_OF_IMAGES; i++)
        FavList[i]=1;  

    // save it
    [self Save];

    // put in all 0's
    for(int i=0; i<AMOUNT_OF_IMAGES; i++)
        FavList[i]=0; 

    // load it
    NSString *path = [NSString stringWithFormat:@"%@/test3.bin", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];
    NSData *read = [NSData dataWithContentsOfFile:path];
    if (read==nil)
    {
        // it has not been save whete, set up a empty favret
        for(int i=0; i<AMOUNT_OF_IMAGES; i++)
            FavList[i]=0;     
    }
    else
    {
    // transfer to our arrow
     NSRange test = {0,sizeof(FavList)};
    [read getBytes:&FavList range:test];

    // what happen, the data is still 0, not 1?????????
   }

} 


-(void)Save{
    // get file name
    NSString *path = [NSString stringWithFormat:@"%@/test3.bin", [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0]];

    NSMutableData *data = [NSMutableData dataWithLength:sizeof(FavList)];
    [data appendBytes:&FavList length:sizeof(FavList)];
    [data writeToFile:path atomically:YES];     

}

1 个答案:

答案 0 :(得分:0)

我使用[data appendBytes:&amp; FavList length:sizeof(FavList)];添加数据。附件在最后添加了数据。它是用FavList的大小创建的,因此乞讨全部为零,数据存储在最后。

当读取数据时,它是乞讨而只是零