I'm trying to take input from user and write it into binary file. This is my code, it runs smoothly but when I try to read the file in another (shows that its NULL) so I'm not sure why the data is not being saved into the file.
Code:
#include <stdio.h>
#include <stdlib.h>
int length=2, width=2;
struct LandData
{
int height;
};
struct LandData* WritingData()
{
FILE *fptr;
struct LandData *arr = (struct LandData*)malloc(length* width* sizeof(struct LandData));
if ((fptr = fopen("data.bin","wb")) == NULL){
printf("Error! opening file");
exit(1);
}
for (int i = 0; i < length ; i++){
for (int j = 0; j < width; j++){
printf("choose height: ");
scanf("%d", &(arr + i*width + j)->height);
fwrite(arr, sizeof(struct LandData), 1, fptr);
}
}
fclose(fptr);
return(arr);
}
int main()
{
struct LandData *arr =WritingData();
free(arr);
return 0;
}