My question is a simple one...I have the following structs declared:
我的问题很简单......我声明了以下结构:
struct Address {
int id;
int set;
char *name;
char *email;
};
struct Database {
struct Address rows[512];
};
struct Connection {
FILE *file;
struct Database *db;
};
Now having that clear, I initialize my "Database" inside my "Connection" with some dummy Addresses. I later take this database and save it into the file inside my "Connection" struct with:
现在说清楚了,我用一些虚拟地址初始化我的“连接”中的“数据库”。我稍后将这个数据库保存到我的“Connection”结构中的文件中:
void Database_write(struct Connection *conn){
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1){
die("Failed to write database.\n",conn);
}
rc = fflush(conn->file);
if(rc == -1){
die("Cannot flush database.\n",conn);
}
Everything works great when I have a predetermined number of rows inside my "Database" struct for my Addresses i.e. 512. But, what if I want to make the number of rows dynamically? As in maybe as a param passed to a function? I have tried using the following...
当我的“数据库”结构中有一些预定数量的行用于我的地址,即512时,一切都很有效。但是,如果我想动态地设置行数,该怎么办?可能作为传递给函数的参数?我尝试过使用以下内容......
struct Database {
struct Address *rows;
};
And allocating space to this pointer with:
并为此指针分配空间:
conn->db->rows = (struct Address*) malloc(sizeof(struct Address)*max_rows);
With max_rows being a param passed to a function...But, now the problem is that when I go and try to save this to the file inside my "Connection" struct I just save the pointer "struct Address *rows;" and not the data with the space allocated to it. Any suggestions as to how to save this allocated space or have a predetermined array inside a struct and then grow it dynamically?
使用max_rows作为传递给函数的参数...但是,现在的问题是,当我去尝试将其保存到我的“Connection”结构中的文件时,我只保存指针“struct Address * rows;”而不是分配给它的空间的数据。有关如何保存此已分配空间或在结构中具有预定数组然后动态增长的任何建议?
Thanks in advance!
提前致谢!
1 个解决方案
#1
3
You are on the right track with malloc for creating a dynamic number of Addresses.
使用malloc可以创建动态数量的地址。
conn->db->rows = (struct Address*) malloc(sizeof(struct Address)*max_rows);
But then you have a problem writing them out to file in Database_write. This is because the dynamically-allocated structure no longer has the number of rows hardwired into it. You will have to change Database_write to
但是,在Database_write中将它们写入文件时遇到问题。这是因为动态分配的结构不再具有硬连线的行数。您必须将Database_write更改为
- Pass in how many rows to write.
- Adjust your fwrite line to write out all the rows.
传递要写入的行数。
调整您的fwrite行以写出所有行。
You had:
void Database_write(struct Connection *conn)
{
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1){
die("Failed to write database.\n",conn);
}
...
You now need something like:
你现在需要这样的东西:
void Database_write(struct Connection *conn, int num_rows)
{
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), num_rows, conn->file);
if(rc != num_rows)
{
die("Failed to write database.\n",conn);
}
...
You could also add the number of rows to your database struct to record how many rows are supposed to be in the file:
您还可以向数据库结构中添加行数,以记录文件中应包含的行数:
struct Database
{
int num_rows;
struct Address *rows;
};
In which case you should fwrite the number of rows to file first, then write num_rows of struct Address.
在这种情况下,您应首先写入要存档的行数,然后写入struct Address的num_rows。
You might also want to look up realloc for changing the number of rows on the fly. Hint - use with care, and pay close attention to the return value.
您可能还希望查找realloc以便动态更改行数。提示 - 小心使用,并密切关注返回值。
#1
3
You are on the right track with malloc for creating a dynamic number of Addresses.
使用malloc可以创建动态数量的地址。
conn->db->rows = (struct Address*) malloc(sizeof(struct Address)*max_rows);
But then you have a problem writing them out to file in Database_write. This is because the dynamically-allocated structure no longer has the number of rows hardwired into it. You will have to change Database_write to
但是,在Database_write中将它们写入文件时遇到问题。这是因为动态分配的结构不再具有硬连线的行数。您必须将Database_write更改为
- Pass in how many rows to write.
- Adjust your fwrite line to write out all the rows.
传递要写入的行数。
调整您的fwrite行以写出所有行。
You had:
void Database_write(struct Connection *conn)
{
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1){
die("Failed to write database.\n",conn);
}
...
You now need something like:
你现在需要这样的东西:
void Database_write(struct Connection *conn, int num_rows)
{
rewind(conn->file);
int rc = fwrite(conn->db, sizeof(struct Database), num_rows, conn->file);
if(rc != num_rows)
{
die("Failed to write database.\n",conn);
}
...
You could also add the number of rows to your database struct to record how many rows are supposed to be in the file:
您还可以向数据库结构中添加行数,以记录文件中应包含的行数:
struct Database
{
int num_rows;
struct Address *rows;
};
In which case you should fwrite the number of rows to file first, then write num_rows of struct Address.
在这种情况下,您应首先写入要存档的行数,然后写入struct Address的num_rows。
You might also want to look up realloc for changing the number of rows on the fly. Hint - use with care, and pay close attention to the return value.
您可能还希望查找realloc以便动态更改行数。提示 - 小心使用,并密切关注返回值。