Writing Integer Array to File in Binary Form and Reading It Into Integer Array

May 30, 2013 at 7:30pm
Both arrays arr and pointers are the same size.
I am having problems reading pointers from file into a new int array. Do I need to cast it somehow?
Thank you.
1
2
3
4
5
6
7
8
9
10
11
FILE* ky_pt=fopen("stashedclient","ab");
write(fileno(ky_pt), pointers, sizeof(pointers) );
pointerindex=lseek(fileno(ky_pt), 0, SEEK_CUR );
printf("pointerindex after writing array %d\n", pointerindex );
printf("size of pointers %d \n", sizeof(pointers) );

int arr[257];
printf(" size of new arr %d \n", sizeof(arr) );
pointerindex=lseek (fileno(ky_pt), -(sizeof(pointers)) , SEEK_END );
printf("about to read arr from index %d\n", pointerindex );
read(fileno(ky_pt) , arr, sizeof(arr));
Last edited on May 30, 2013 at 7:31pm
May 30, 2013 at 7:49pm
You cannot save pointers to a file because when you load the file your program's memory layout will be different and the data those pointers pointed to will have been gone for eons.
May 30, 2013 at 7:49pm
pointers is just an array of integers.
Topic archived. No new replies allowed.