2

I'm trying to make a function that auto populates data into a binary file if the file does not exist. Since it doesn't exist, I want to add data myself from a function so it can also create the file itself with the data.

The struct I made

struct Address {
    char city[ADDR_SIZE];
    char state[ADDR_SIZE];
    int zipcode;
};
struct Record {
    char name[NAME_SIZE];
    int socialSecurityNum;
    int dateOfBirth;
    Address address;
};

I'm trying to add the data like this

dataFile.seekp(getBytePosition(0), ios::beg);
citizen.name = "Some name"; //ERROR: char[30] not assignable
citizen.socialSecurityNum = 123123123;
citizen.address.zipcode = 75042;
dataFile.write(reinterpret_cast<char *>(&citizen), sizeof(citizen));

Giving it integers is simple, but how do I make it populate the char name array?

I also tried these but failed

citizen.name[NAME_SIZE] = "Some name"; //Fails
citizen.name[NAME_SIZE] = {"Some Name"}; //Fails
citizen.name[NAME_SIZE] = 'Some Name'; //Too Many errors

5 Answers 5

3

First option (not recommended): Use strcpy

strcpy(citizen.name, "Some name");

or the more safer (recomended) strncpy

strncpy(citizen.name, "Some name", sizeof(citizen.name));

Second option (recomended): Instead of char array use a std::string:

struct Address {
    std::string city;
    std::string state;
    int zipcode;
};
struct Record {
    std::string name;
    int socialSecurityNum;
    int dateOfBirth;
    Address address;
};

With std::string you can assign to it:

citizen.name = "Some name";

However you'll have the additional burden of serializing your data but I believe it worths it.

Sign up to request clarification or add additional context in comments.

6 Comments

What's wrong with strcpy? If I remember, I did try them as strings first, but then I had to use cin.getline to write to a binary file and I believe I was getting errors. I've come a long way since and it would take a while to switch it all back to string.
@Jozemite:Apps: strcpy() does not perform bounds checking so can cause buffer overflows if used incorrectly. Use strncpy() or equivalent instead.
@101010: The use of std::string is fine for manipulating data in memory, but if you put a std::string in a struct, you can no longer write that struct as-is to a file, or read it back. You have to serialize the data into a flat format instead. That is more work. Using a fixed array allows for direct file I/O.
@JozemiteApps Well if you're careful there's nothing wrong. The problem is that using char arrays and their C facilities could cause buffer overflows. Using std::string is the recommend way to store and manipulate strings, unless you're programming in a very strict environment in terms of hardware resources.
@RemyLebeau Yes that's true. Nevertheless, I believe it worths the trouble.
|
1

Use strcpy

strcpy(citizen.name, "Some name") ;

Use of std::string would simplyfy code but would make loading/saving to file more difficult since std::ostream.write would not be a bit more complex

Comments

1

Try using strcpy or memcpy with the appropriate casts.

Check this.

Comments

1

As you have a char array, you can't just assign values with =. You should use either strcpy() or better, std::copy()

Comments

1

You cannot assign a char* to a char[] array after the array has been declared. Use strncpy() instead to copy the content of the char* data into the char[] memory:

strncpy(citizen.name, "Some name", NAME_SIZE);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.