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 8564431
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T17:11:57+00:00 2026-06-11T17:11:57+00:00

gcc (GCC) 4.7.0 c89 Hello, I have trying to save a database of rows

  • 0
gcc (GCC) 4.7.0
c89

Hello,

I have trying to save a database of rows to a file (db.dat) using fwrite to write it and retrieve it using fread. However, when I retrive it with fread the data is empty.

I not sure if I should malloc some memory before getting the data. Which I have tried in my example

I am not showing free memory here, as this is just the code snippets.

Many thanks for any suggestions,

This is my structure of the database:
struct address {
    int id;
    int set;
    char *name;
    char *email;
};

struct database {
    struct address **rows;
    size_t data_size;
    size_t max_size;
};

struct connection {
    FILE *fp;
    struct database *db;
};

Database open:

struct connection* database_open(const char *filename, char mode)
{
    struct connection *conn = NULL;

    conn = malloc(sizeof(struct connection));

    conn->db = malloc(sizeof(struct database));
    if(conn->db == NULL) {
        log_exit("Memory error allocating database", conn);
    }

    if(conn == NULL) {
        log_exit("Memory error allocating connection", conn);
    }

    printf("Create a new database\n");
    conn->fp = fopen(filename, "w");

    if(conn->fp == NULL) {
        log_exit("Failed to open file", conn);
    }

    return conn;
}

creating a database:

void database_create(struct connection *conn, int _max_size, int _data_size)
{
    int i = 0;
    struct address *db_row = NULL;

    /* Set the size of the database by assigning the max number of database rows */
    conn->db->max_size = _max_size;
    conn->db->data_size = _data_size;

    /* Allocate memory for rows and size */
    conn->db->rows = malloc(sizeof(struct address) * _max_size);
    if(!conn->db->rows) {
        log_exit("Memory error alocating rows", conn);
    }

    for(i = 0; i < _max_size; i++) {
        db_row = malloc(sizeof(struct address));

        db_row->id = i;
        db_row->set = 0;
        db_row->name = malloc(_data_size);
        db_row->email = malloc(_data_size);

        /* Add the row to the database */
        conn->db->rows[i] = db_row;
    }
}

Writing the database

void database_write(struct connection *conn)
{
    rewind(conn->fp);

    if(fwrite(conn->db, sizeof(struct database), 1, conn->fp) != 1) {
        log_exit("Failed to write to database.", conn);
    }

    if(fflush(conn->fp) != 0) 
    {
        log_exit("Cannot flush database.", conn);
    }
}

Then I then open the database for r+ and use fread. However, my data that I have set in the id and set variables are empty.

void database_load(struct connection *conn)
{
    /* Clear any errors from file pointer */
    clearerr(conn->fp);

    /* I have had to hack this just so that I can get the
       memory allocated before filling the structure not sure if this is the best way */
    conn->db->rows = malloc(sizeof(struct address) * 1); /* This would be set to how many rows I have - just experimenting with just one */
    conn->db->rows[0] = malloc(sizeof(struct address));
    conn->db->rows[0]->name = malloc(20);
    conn->db->rows[0]->email = malloc(20);

    if(fread(conn->db, sizeof(struct database), 1, conn->fp) != 1) {
        if(feof(conn->fp) != 0) {
            log_exit("Database end of file", conn);
        }

        if(ferror(conn->fp) != 0) {
            log_exit("Database cannot open", conn);
        }
    }    
}

==== EDIT ===

int main(int argc, char **argv)
{
    char *filename = NULL;
    char action = 0;
    int id = 0;
    int rows = 0;
    int size = 0;
    struct connection *conn = NULL;

    if(argc < 3) {
        log_exit("USAGE: ex17 <dbfile> <action> [ action params ]", conn);
    }

    filename = argv[1];
    action = argv[2][0];
    rows = atoi(argv[3]);
    size = atoi(argv[4]);

    conn = database_open(filename, action);

    if(argc > 3) {
        id = atoi(argv[3]);
    }

    switch(action) {
    case 'c':
        database_create(conn, rows, size);
        database_write(conn);
        break;

    case 's':
        if(argc != 6) {
            log_exit("Need id, name, email to set", conn);
        }

        database_set(conn, id, argv[4], argv[5]);
        database_write(conn);
        break;

    default:
        log_exit("Invalid action, only: c = create, g = get, d = del, l = list", conn);
    }

    database_close(conn);

    return 0;
}
  • 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-06-11T17:11:59+00:00Added an answer on June 11, 2026 at 5:11 pm

    Seeing your rows is a double pointer, you need to initialize the first pointer with the sizeof(pointer*), this:

    conn->db->rows = malloc(sizeof(struct address) * 1); 
    

    should be:

    conn->db->rows = malloc(sizeof(struct address*));  //notice how struct address becomes struct address*
    

    Same with this:

    conn->db->rows = malloc(sizeof(struct address) * _max_size);
    

    should be:

      conn->db->rows = malloc(sizeof(struct address*) * _max_size);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

gcc (GCC) 4.7.0 c89 Hello, I have the following structure that I am trying
gcc 4.4.2 c89 I have a wave file: 8000 Hz 16 bit I am
gcc 4.7.2 c89 Hello, I am using posix message queues: mq_create, mq_send, mq_receive, etc.
gcc 4.7.2 c89 Hello, I am using the APR safe thread queue in the
Possible Duplicate: Should a function have only one return statement? Hello, gcc 4.4.4 c89
gcc (GCC) 4.7.2 c89 Hello, I have been looking at a test suite and
gcc 4.7.2 c89 Hello, I am using the Apache Portable Runtime and looking at
gcc 4.7.2 c89 Hello, I am working on some maintainance and I have the
gcc (GCC) 4.7.0 c89 x86_64 Hello, I am wondering is it worth it using
gcc 4.7.2 c89 APR 1.4 Hello, I am using APR thread pools, event loop

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.