Question: How to read structs from file and store them in array (c programming) i have an array called flights that stores structs of type flight.

How to read structs from file and store them in array (c programming)

i have an array called flights that stores structs of type flight. My program is able to store up to 5 flights in memory and i am able to call a write function that writes these structs into a binary file using fwrite(flights, sizeof(flight_t), size, fp) where size tracks the index of the flight array up until 5. I am now trying to read the structs that have been written into the file using fread(&flight, sizeof(flight_t), size, fp) within a for loop (i = 0; i

This doesnt work as my display function that prints out the flights in memory prints("no flights") meaning the array is empty. So my question in the end is how do i read the structs from a file by calling a function and store them into my array so that it works?

Further info: my flights array and size variable are declared in main but i want to call the read and write functions

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!