I have a array define as short FavList[300];
I try to load all 1’s in it, save it.
Then out in all 0’s and load it.
After I read in thew data, it is still all 0.s, and did not get loaded with all ones.
code
-(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];
}
I use [data appendBytes:&FavList length:sizeof(FavList)]; to add the data. The append added the data at the end. It was created with a size of FavList, thus the begging was all zeros with the data stored at the end.
When the data was read in it was the begging and just zeros