【问题标题】:C Programming with ARM - Output and input of struct to a file使用 ARM 进行 C 编程 - 将结构输出和输入到文件
【发布时间】:2011-07-05 08:16:40
【问题描述】:

我正在编写的程序中的结构有点问题。 背景;我正在 ARMv7 芯片上用 C 语言开发代码。该系统有一个带有 FAT16 文件系统的 SD 卡,用于文件访问。 我正在使用一个用于 FAT16 文件读写操作的库来处理文件输出/输入的任何工作。 目前我在 main 函数之前全局声明一个结构体;

struct config_information
{
    char *name;

    //version
    char *fwversion;

    //IP address
    char *ip;
};

//Declare structs for config information
struct config_information config;
struct config_information loadedconfig;

指针用于字符串。 例如,我使用 &config.name 将数据读入这些变量。我必须使用我编写的函数将通过串行端口接收的字符读入另一个指向 strcat 到另一个的字符指针,但这可以正常工作,并且我已经验证了用户输入的详细信息与记录的输入相匹配(有一个快速如果有人想知道,终端窗口上的提示屏幕会提示您输入详细信息)。

然后我实现提供的库函数以将结构保存到文件中;

fat16_write_file(CONF_FILE,&config, READBUFSIZE);

其中 CONF_FILE 只是要使用的文件的文件句柄,&config 是输出缓冲区,READBUFSIZE 是缓冲区的大小(定义为 148,对我的目的来说足够大,但稍后我希望更改它以计算大小结构的输出,然后计算文件大小以便能够正确读取它)。我已经添加了库提供的函数定义,并在末尾添加了开头语句以供参考,以防我不清楚。

这很完美,文件中的输出是; 名称1.1 192.168.1.1 有很多空白。显然我输入 Name 作为名称,1.1 作为固件,IP 是不言自明的。

我天真地以为我可以反过来将它读回我用 this 声明的 loadconfig 结构;

fat16_read_file(CONF_FILE,&loadedconfig,READBUFSIZE);

显然,这没有希望工作,因为结构没有任何预定义的大小来知道要读回多少。

那么,我怎样才能将我保存的数据读回到它来自的同一个结构中呢? 这个名字可以是任何大小,不是很大,但不能安全地预测。固件版本永远只会是#.#,而且 IP 显然在已经定义的限制范围内。

感谢您在正确方向上的任何提示或轻推。请询问您是否需要更多信息来了解范围。

供参考;

/**
 * \ingroup fat16_file
 * Reads data from a file.
 *
 * The data requested is read from the current file location.
 *
 * \param[in] fd The file handle of the file from which to read.
 * \param[out] buffer The buffer into which to write.
 * \param[in] buffer_len The amount of data to read.
 * \returns The number of bytes read, 0 on end of file, or -1 on failure.
 * \see fat16_write_file
 */
int16_t fat16_read_file(struct fat16_file_struct* fd, uint8_t* buffer, uint16_t buffer_len)

/**
 * \ingroup fat16_file
 * Writes data to a file.
 *
 * The data is written to the current file location.
 *
 * \param[in] fd The file handle of the file to which to write.
 * \param[in] buffer The buffer from which to read the data to be written.
 * \param[in] buffer_len The amount of data to write.
 * \returns The number of bytes written, 0 on disk full, or -1 on failure.
 * \see fat16_read_file
 */
int16_t fat16_write_file(struct fat16_file_struct* fd, const uint8_t* buffer, uint16_t buffer_len)

【问题讨论】:

    标签: c file-io struct arm fat


    【解决方案1】:

    如果您希望将结构写出到文件中然后再将其读回,则不应为其成员使用指针。你应该这样声明它:

    /* specify sizes according to your needs */
    #define MAX_NAME_SIZE 16
    #define MAX_FWVERSION_SIZE 16
    #define MAX_IP_SIZE 16
    
    struct config_information
    {
        char name[MAX_NAME_SIZE];
    
        //version
        char fwversion[MAX_FWVERSION_SIZE];
    
        //IP address
        char ip[MAX_IP_SIZE];
    };
    
    // write it to a file
    fat16_write_file(CONF_FILE,&config, sizeof(config_information));
    
    // read it from a file
    fat16_read_file(CONF_FILE,&loadedconfig, sizeof(config_information));
    

    【讨论】:

    • 感谢您的回答。我之前尝试过与上述类似的方法,但运气不佳。我刚刚尝试了您在上面发布的内容(将 sizeof 调用更改为 sizeof(struct config_information)),但它仍然没有将任何内容读入到 loadconfig 结构中。更改后,它仍然像以前一样正确保存数据(由于最大尺寸的定义而具有不同的尺寸)。
    • 对,事实证明,每次SD卡进行同步操作时,文件描述符都可以更改。在我上面的例子中,我分配了文件描述符,写出文件,然后尝试读回它。我需要做的就是在再次读取操作之前打开文件。
    猜你喜欢
    • 2016-09-19
    • 2016-07-31
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-11-19
    相关资源
    最近更新 更多