使用 ARM 进行 C 编程 - 将结构输出和输入到文件

发布于 2024-10-19 05:44:07 字数 2303 浏览 4 评论 0原文

我正在编写的程序中的结构有一些问题。 背景;我正在 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是不言自明的。

我天真地认为我可以做相反的事情,将其读回到我用此声明的 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)

I am having a bit of a problem with a struct within a program I am writing.
Background; I am developing code in C on an ARMv7 chip. The system has an SD card with FAT16 filesystem for file access.
I am using a library for FAT16 file read and write operations to do any work with file output/input.
Currently I am declaring a struct globally before the main function;

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;

The pointers are for string use.
I read the data into these variables using &config.name for example. I have to use a function I have written to read in characters received over the serial port into another char pointer to strcat to another, but this works correctly and I have verified the details input by a user match the recorded input (there is a quick prompt screen on a terminal window to prompt for details if anyone was wondering).

I then implement the provided library function to save the struct to a file;

fat16_write_file(CONF_FILE,&config, READBUFSIZE);

Where CONF_FILE is just the file handle for the file to use, &config is an output buffer and READBUFSIZE is the size of the buffer (defined as 148, big enough for my purposes but later I hope to change it to calculate the size of the struct to output, and then calculate the file size to be able to read it back in correctly). I have added the function definition provided by the library with the opening statement at the end for reference in case I haven't been clear.

This works perfectly, the output in the file is;
Name1.1 192.168.1.1
with alot of whitespace. Obviously I input Name as the Name, 1.1 as the firmware and the IP is self explanatory.

Naively I thought I could do the reverse to read it back into the loadconfig struct I declared with this;

fat16_read_file(CONF_FILE,&loadedconfig,READBUFSIZE);

Clearly this has no hope of working as the struct doesn't have any pre-defined sizes for it to know how much to read back in.

So, how can I read the data I have saved back into the same struct that it came from?
The name could be of any size, not massive but it cannot be safely predicted. The FW version will only ever be #.# and the IP is obviously within already defined limits.

Thanks for any hints or nudges in the right direction. Please ask if you feel you need more information to understand the scope.

For reference;

/**
 * \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)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

审判长 2024-10-26 05:44:07

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

/* 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));

If you wish to write the struct out to a file then read it back in, you should not use pointers for its members. You should declare it like so:

/* 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));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文