我有一个数组定义为短 FavList[300];
我尝试在其中加载所有 1,保存它。然后全0并加载它。
在我读入 w 数据后,它仍然是 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];
}