I am writing a program to save some object (struct) to a buffer. I don't have experiment to write many objects to buffer and read these objects from the buffer. Any help would be appreciated. My code can write one item to object and I want write many objects to the buffer
struct PointFull {
double lat;
double lon;
};
PointFull item1;
PointFull item2;
PointFull item3;
PointFull item4;
PointFull item5;
void* buffer = malloc(sizeof (PointFull));
int fd = open("output", O_WRONLY | O_CREAT, S_IWUSR | S_IRUSR);
if (fd < 0) {
printf("Error opening file\n");
return 1;
}
//The below function can write only one item to buffer. How to write 5 item (from item1 to item5) to the buffer
//memcpy(buffer, &item1, sizeof (item));
write(fd, buffer, sizeof (item));
Now I have a file named "output" in hard disk and then I want read the file to test data.
int fd2 = open("output", O_RDONLY, S_IWUSR | S_IRUSR);
if (fd2 < 0) {
printf("Error opening file\n");
return 1;
}
void* bufferRead;
bufferRead = malloc(5* sizeof (PointFull));
read(fd2, bufferRead,sizeof (PointFull));
At the moment, I have bufferRead contain 5 items but I dont know how to read buffer to insert data to struct??? Plz help me!