在C中存储和恢复文件中的struct

Joe*_*ann 3 c struct file restore

我需要在一个文件中存储一个结构并将其读回来然后返回它.我会尝试将其写入文件,如下所示:

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);
}
Run Code Online (Sandbox Code Playgroud)

那会有用吗?我该怎么读回来并从这个函数返回呢?

struct cal lld_tpReadCalibration(void) {

}
Run Code Online (Sandbox Code Playgroud)

结构是:

   struct cal {
       float xm; 
       float ym; 
       float xn; 
       float yn; 
   };
Run Code Online (Sandbox Code Playgroud)

谢谢你的帮助.

tom*_*ahh 5

您可以像存储结构一样检索结构.

read(&fdst, &cal, sizeof(cal));
Run Code Online (Sandbox Code Playgroud)

但是你必须要小心,因为有关于endianess的问题,你无法在每个架构上都这样做.