Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4014970
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T09:33:42+00:00 2026-05-20T09:33:42+00:00

I am having a bit of a problem with a struct within a program

  • 0

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)
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-20T09:33:42+00:00Added an answer on May 20, 2026 at 9:33 am

    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));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Having a bit of a problem here on a matrix multiplication code. I seem
I'm having a bit of a problem with the following code. I've seen tons
Ok so im having a bit of an issue here. basically Im reading shared
I am running the following code on a 64-bit Ubuntu box with 18 GB
I've read quite a bit about initialization of static class variables in c++ and
not sure if this is possible without having to go through several passes, but
Is there a trick to get the safe bool idiom completely working without having
Currently I want to optimize my 3d engine for consoles a bit. More precisely
I'm writing an application updater that pulls installation package from our distribution web site
I was wondering why Nullable<T> is a value type, if it is designed to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.