Reputation: 2282
I need to store a struct in a file and read it back to return it then. I would try to write it to the file like this:
void lld_tpWriteCalibration(struct cal cal) {
FIL fdst; /* file objects */
UINT bw; /* File write count */
/* Create destination file on the drive 0 */
wf_open(&fdst, "0:calibration.txt", FA_CREATE_ALWAYS | FA_WRITE);
wf_write(&fdst, cal, sizeof(cal), &bw);
wf_close(&fdst);
}
Would that work? And how can I read it back and return it from this function?
struct cal lld_tpReadCalibration(void) {
}
The struct is:
struct cal {
float xm;
float ym;
float xn;
float yn;
};
Thanks for your help.
Upvotes: 3
Views: 412
Reputation: 13651
You can retrieve your structure the same way you stored it.
read(&fdst, &cal, sizeof(cal));
But you got to be careful, you won't be able to do this on every architecture because of endianess problem.
Upvotes: 5
Reputation: 753555
You'll be OK with that structure and that writing/reading technique if you only try to read the file on the same type of machine as you write it on. The data written like that is not reliably portable across different types of machines.
Upvotes: 1