当前位置: 首页>>代码示例>>C++>>正文


C++ Database_list函数代码示例

本文整理汇总了C++中Database_list函数的典型用法代码示例。如果您正苦于以下问题:C++ Database_list函数的具体用法?C++ Database_list怎么用?C++ Database_list使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。


在下文中一共展示了Database_list函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。

示例1: main

int main(int argc, char *argv[])
{
  if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");

  char *filename = argv[1];
  char action = argv[2][0];
  struct Connection *conn = Database_open(filename, action);
  int id = 0;
  char *name = "\0";

  if(strcmp(argv[2],"f")==0){
    name = argv[3];
  }
  else if(argc > 3) {
    id = atoi(argv[3]);
  }
  
  if(id >= MAX_ROWS) die("There's not that many records");

  switch(action) {
  case 'c':
    Database_create(conn);
    Database_write(conn);
    break;
  case 'g':
    if(argc != 4) die("Need an id to get");

    Database_get(conn, id);
    break;
  case 'd':
    if(argc != 4) die("Need id to delete");

    Database_delete(conn, id);
    Database_write(conn);
    break;

  case 's':
    if(argc != 6) die("need id, name, email to set");

    Database_set(conn, id, argv[4], argv[5]);
    Database_write(conn);
    break;

  case 'l':
    Database_list(conn);
    break;

  case 'f':
    Database_find(conn, name);
    break;

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

  Database_close(conn);
  
  return 0;
}
开发者ID:imnotthomas,项目名称:cthehardway,代码行数:59,代码来源:ex17.c

示例2: main

int main(int argc, char *argv[]) {
if (argc < 3)
die("USAGE: ex17 <dbfile> <action> [action params]");
char *filename = argv[1];
char action = argv[2][0];
Database_open(filename, action);
int id = 0;
if (action != 'c' && action != 'f' && argc > 3) {
id = atoi(argv[3]);
if (id >= conn->db->max_rows)
	die("There are not that many records.");
}
switch (action) {
// Create
case 'c':
if (argc != 5)
	die("MAX_DATA and MAX_ROWS required.");
conn->db->max_data = atoi(argv[3]);
conn->db->max_rows = atoi(argv[4]);
Database_create();
Database_write();
break;
// Get
case 'g':
if (argc != 4)
	die("Need an ID to get.");
Database_get(id);
break;
// Set
case 's':
if (argc != 6)
	die("Need ID, name, and email to set.");
Database_set(id, argv[4], argv[5]);
Database_write();
break;
// Find
case 'f':
if (argc != 4)
	die("Need a name or email to search for.");
Database_find(argv[3]);
break;
// Delete
case 'd':
if (argc != 4)
	die("Need ID to delete.");
Database_delete(id);
Database_write();
break;
// List
case 'l':
Database_list();
break;
default:
die("Invalid action, only: c=create, g=get, s=set, d=delete, l=list");
}
Database_close();
return 0;
}
开发者ID:edek437,项目名称:justcode,代码行数:58,代码来源:ex17noglob.c

示例3: main

int main(int argc, char *argv[])
{
	if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]", NULL);

	int max_data = MAX_DATA;
	int max_rows = MAX_ROWS;
	char *filename = argv[1];
	char action = argv[2][0];
	struct Connection *conn = Database_open(filename, action);
	int id = 0;

	if(argc > 3) id = atoi(argv[3]);
	if(id >= MAX_ROWS) die("There's not that many records.", conn);

	switch(action) {
		case 'f':
			if(argc != 4) die("Need a name to find", conn);
			Database_find(conn, argv[3]);
			break;

		case 'c':
			max_data = argv[3] ? atoi(argv[3]) : MAX_DATA;
			max_rows = argv[4] ? atoi(argv[4]) : MAX_ROWS;
			Database_create(conn, max_data, max_rows);
			Database_write(conn);
			break;

		case 'g':
			if(argc != 4) die("Need an id to get", conn);
			Database_get(conn,id);
			break;

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

			Database_set(conn, id, argv[4], argv[5]);
			Database_write(conn);
			break;

		case 'd':
			if(argc != 4) die("Need id to delete", conn);

			Database_delete(conn, id);
			Database_write(conn);
			break;

		case 'l':
			Database_list(conn);
			break;
		default:
			die("Invalid action, only: c=create, g=get, s=set, d=del, l=list", conn);
	}

	Database_close(conn);

	return 0;
}
开发者ID:paulogeyer,项目名称:lcthw,代码行数:57,代码来源:ex17.c

示例4: main

int main(int argc, char *argv[])
{
  char usage[64];
  sprintf(usage, "USAGE: %s <dbfile> <action> [action params]", basename(argv[0]));

  if(argc < 3) die(usage, NULL);

  char *filename = argv[1];
  char action = argv[2][0];
  struct Connection *conn = Database_open(filename, action);
  int id = 0;

  if(argc > 3) id = atoi(argv[3]);
  if(id >= MAX_ROWS) die("There's not that many records.", conn);

  switch(action) {
    case 'c':
      Database_create(conn);
      Database_write(conn);
      break;

    case 'g':
      if(argc != 4) die("Need an id to get", conn);

      Database_get(conn, id);
      break;

    case 's':
      if(argc != 7) die("Need id, name, email, and street to set", conn);

      Database_set(conn, id, argv[4], argv[5], argv[6]);
      Database_write(conn);
      break;

    case 'd':
      if(argc != 4) die("Need id to delete", conn);

      Database_delete(conn, id);
      Database_write(conn);
      break;

    case 'f':
      if(argc != 4) die("Need something to find", conn);
      Database_find(conn, argv[3]);
      break;

    case 'l':
      Database_list(conn);
      break;
    default:
      die("Invalid action, only: c=create, g=get, s=set, d=del, l=list, f=find", conn);
  }

  Database_close(conn);

  return 0;
}
开发者ID:meatballhat,项目名称:box-o-sand,代码行数:57,代码来源:ex17-ec.c

示例5: main

int main(int argc, char *argv[])
{
	if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]", NULL);

	char *filename = argv[1];
	char action = argv[2][0];
	struct Connection *conn = Database_open(filename, action);
	int id = 0;

	if((argc > 3) && (action != 'c') && (action != 'f')) id = atoi(argv[3]);
	//if(id >= conn->db->MAX_ROWS) die("There's not that many records.", conn);

	switch(action) {
		case 'c':
			Database_create(conn, atoi(argv[3]), atoi(argv[4]));
			Database_write(conn);
			break;

		case 'g':
			if(argc != 4) die("Need an id to get", conn);

			Database_get(conn, id);
			break;

		case 's':
			if(argc != 7) die("Need id, name, email and age to set", conn);

			Database_set(conn, id, argv[4], argv[5], atoi(argv[6]));
			Database_write(conn);
			break;

		case 'd':
			if(argc != 4) die("Need an id to delete", conn);

			Database_delete(conn, id);
			Database_write(conn);
			break;

		case 'l':
			Database_list(conn);
			break;

		case 'f':
			Database_find(conn, argv[3], argv[4]);
			break;

		default:
			die("Invalid action, only: c=create, g=get, s=set, d=delete, l=list", conn);
	}

	Database_close(conn);

	return 0;
}
开发者ID:osynetskyi,项目名称:hardc,代码行数:54,代码来源:ex17.c

示例6: main

int main(int argc, char *argv[])
{
	if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");
	
	char *filename = argv[1];
	char action = argv[2][0];
	Database_open(filename, action);	
	int id = 0;
	
	if(argc > 3) id = atoi(argv[3]);
	if(id >= MAX_ROWS) die("There's not that many records.");
	
	switch(action) {
		case 'c':
			Database_create();
			Database_write();
			break;
			
		case 'g':
			if(argc != 4) die("Need an id to get");
			
			Database_get(id);
			break;
		
		case 's':
			if(argc != 6) die("Need id, name, email to set");
			
			Database_set(id, argv[4], argv[5]);
			Database_write();
			break;
			
		case 'd':
			if(argc != 4) die("Need an id to delete)");
			
			Database_delete(id);
			Database_write();
			break;
			
		case 'l':
			Database_list();
			break;
		
		case 'f':
			Database_find(argv[3]);
			break;
		default:
			die("Invalid action, only c=create, g=get, s=set, d=del, l=list");
	}
	
	Database_close();
	return 0;
}
开发者ID:smwhit,项目名称:LearnCTheHardWay,代码行数:52,代码来源:ex17a.c

示例7: main

int main(int argc, char *argv[])
{
    char *filename = argv[1];
    char action = argv[2][0];
    struct Connection *conn = Database_open(filename, action);
    int id = 0;

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

    if(argc > 3) id = atoi(argv[3]);
    if(id >= MAX_ROWS) die("There's not that many records.", conn);

    switch(action) {
    case 'c':
        if(argc == 5) Database_create(conn, atoi(argv[3]), atoi(argv[4]));
        else Database_create(conn, 0, 0);
        Database_write(conn);
        break;

    case 'g':
        if(argc != 4) die("Need an id to get", conn);

        Database_get(conn, id);
        break;

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

        Database_set(conn, id, argv[4], argv[5]);
        Database_write(conn);
        break;

    case 'd':
        if(argc != 4) die("Need id to delete", conn);

        Database_delete(conn, id);
        Database_write(conn);
        break;

    case 'l':
        Database_list(conn);
        break;

    default:
        die("Invalid action, only: c=create, g=get, s=set, d=del, l=list, r=resize", conn);
    }

    Database_close(conn);

    return 0;
}
开发者ID:tnesbit450,项目名称:learning,代码行数:51,代码来源:ex17.c

示例8: ui

int ui(int argc, char *argv[])
{
    struct Connection *conn = NULL;
    char action = 'r';
    char *filename;
    char *name = "the dude";
    char *email = "[email protected]";
    int id = 0;
    while (1) {
        fputs("$ ", stdout);
        char cmd = getchar();
        switch (cmd) {
            case 'c':
                Database_create(conn);
                Database_write(conn);
                break;

            case 'd':
                Database_delete(conn, id);
                Database_write(conn);
                break;

            case 'g':
                Database_get(conn, id);
                break;

            case 'h': 
                help(); 
                break;

            case 'l':
                Database_list(conn);
                break;

            case 'o': 
                conn = Database_open(filename, action);
                break;

            case 's':
                Database_set(conn, id, name, email);
                Database_write(conn);
                break;

            case 'q': goto exit;
            default:  break;
        }
    }
exit:
    return 0;
}
开发者ID:walrus7521,项目名称:code,代码行数:50,代码来源:dbase.c

示例9: main

int main(int argc, char *argv[])
{
    if(argc < 3) die("USAGE: mydb <dbfile> <action> [action params]");

    char *filename=argv[1];
    char action = argv[2][0];

    // will open file. Only creates file if action is 'c'
    struct Connection *conn = Database_open(filename, action);

    int id = 0;

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

    if(id >= MAX_ROWS) die("There's not that many records.");

    switch(action) {
        case 'c':
            Database_create(conn);
            Database_write(conn);
            break;
        case 'g':
            if (argc != 4) die("Need an id to get");
            Database_get(conn, id);
            break;
        case 's':
            if (argc != 6) die("Need id, name, email to set");
            Database_set(conn, id, argv[4], argv[5]);
            Database_write(conn);
            break;
        case 'l':
            Database_list(conn);
            break;
        default:
            die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
    }

    Database_close(conn);

    return 0;
}
开发者ID:baloi,项目名称:c_me,代码行数:41,代码来源:mydb.c

示例10: main

int main(int argc, char *argv[])
{

    if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");
    
    char *filename = argv[1];
    char action = argv[2][0];
    int id = 0;    
    char *email;

    if (action == 'c') {    
        if (argc != 5) {
            die("Need max_rows and max_data");
        }
        max_rows = atoi(argv[3]);
        max_data = atoi(argv[4]);
    }

    Database_open(filename, action);

    if(action != 'c' && argc > 3) {
        if (action != 'f') {
            id = atoi(argv[3]);
        } else {
            email = argv[3];
        }
    }
    if(id >= max_rows) die("There's not that many records.");

    switch(action) {
        case 'c':
            
            //Database_create();
            Database_write();
            break;

        case 'g':
            if(argc != 4) die("Need an id to get");

            Database_get(id);
            break;

        case 'f':
            if (argc != 4) die("Need email to find");

            Database_find(email);
            break;

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

            Database_set(id, argv[4], argv[5]);
            Database_write();
            break;

        case 'd':
            if(argc != 4) die("Need id to delete");

            Database_delete(id);
            Database_write();
            break;

        case 'l':
            Database_list();
            break;
        default:
            die("Invalid action, only: c=create, g=get, f=find, s=set, d=del, l=list");
    }

    Database_close();

    return 0;
}
开发者ID:nebffa,项目名称:lcthw,代码行数:73,代码来源:17.c

示例11: Database_create

void Database_create(struct Connection * connection) {
  int i = 0;

  for(i = 0; i < MAX_ROWS; i++) {
    struct Address addr     = { .id = i, .set = 0 };
    connection->db->rows[i] = addr;
  }
}

void Database_set(struct Connection * connection, int id, const char * name, const char * email) {
  struct Address *address = &connection->db->rows[id];
  if(address->set) die("Already set. Delete it first.");

  address->set = 1;
  // Warning BUG
  char * res = strncpy(address->name, name, MAX_DATA);
  // Demonstrate strncpy bug
  if(!res) die("Name copy failed");

  res = strncpy(address->email, email, MAX_DATA);
  if(!res) die("Email copy failed");
}

void Database_get(struct Connection * connection, int id) {
  struct Address * address = &connection->db->rows[id];

  if(address->set) {
    Address_print(address);
  } else {
    die("ID is not set");
  }
}

void Database_delete(struct Connection * connection, int id) {
  struct Address address = { .id = id, .set = 0 };
  connection->db->rows[id] = address;
}

void Database_list(struct Connection * connection) {
  int i = 0;
  struct Database * db = connection->db;

  for(i = 0; i < MAX_ROWS; i++) {
    struct Address * current = &db->rows[i];

    if(current->set) Address_print(current);
  }
}

int main(int argc, char * argv[]) {

  if(argc < 3) die("USAGE: ex17 <db_file> <action> [action params]");

  char * filename = argv[1];
  char   action   = argv[2][0];

  struct Connection * connection = Database_open(filename, action);
  int id = 0;

  if(argc > 3) id = atoi(argv[3]);
  if(id >= MAX_ROWS) die("There's not that many records");

  switch(action) {
    case 'c':
      Database_create(connection);
      Database_write(connection);
      break;

    case 'g':
      if(argc != 4) die("Need an id to get");

      Database_get(connection, id);
      break;

    case 's':
      if(argc != 6) die("Need id, name, and email to set");

      Database_set(connection, id, argv[4], argv[5]);
      Database_write(connection);
      break;

    case 'd':
      if(argc != 4) die("Need id to delete");

      Database_delete(connection, id);
      Database_write(connection);
      break;

    case 'l':
      Database_list(connection);
      break;

    default:
      die("Invalid action. Only: c=create, g=get, s=save, d=delete, l=list");
  }

  Database_close(connection);

  return 0;
}
开发者ID:stevegraham,项目名称:lcthw,代码行数:100,代码来源:ex17.c

示例12: Database_create

void Database_create(struct Connection *conn)
{
	int i = 0;
	for(i = 0; i < MAX_ROWS; i++){
		//Make a prototype
		struct Address addr = {.id = i, .set = 0};
		conn->db->rows[i] = addr;
	}
}

void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
	struct Address *addr = &conn->db->rows[id];
	if(addr->set) die("Already set!");
	addr->set = 1;
	char *res = strncpy(addr->name, name, MAX_DATA);
	if(!res) die("Name copy failed");
	
	res = strncpy(addr->email, email, MAX_DATA);
	if(!res) die("Email copy failed");
}

void Database_get(struct Connection *conn, int id)
{
	struct Address *addr = &conn->db->rows[id];
	
	if(addr->set){
		Address_print(addr);
	} else {
		die("ID not set!");
	}
}

void Database_delete(struct Connection *conn, int id)
{
	struct Address addr = {.id = id, .set = 0};
	conn->db->rows[id] = addr;
}

void Database_list(struct Connection *conn)
{
	int i = 0;
	struct Database *db = conn->db;
	for(i = 0; i < MAX_ROWS; i++){
		struct Address *curr = &db->rows[i];
		if(curr->set){
			Address_print(curr);
		}
	}
}


int main(int argc, char *argv[]){
	
	if(argc < 3) die("USAGE: heapstack <dbfile> <action> [action params]");
	
	char *filename = argv[1];
	char action = argv[2][0];
	struct Connection *conn = Database_open(filename, action);
	int id = 0;
	if(argc > 3) id = atoi(argv[3]);
	if(id >= MAX_ROWS) die("Not enough records");
	switch(action) {
		case 'c':
			Database_create(conn);
			Database_write(conn);
			break;
		case 'g':
			if(argc != 4) die("Need an id");
			Database_get(conn, id);
			break;
		case 's':
			if(argc != 6) die("Need id, name and email");
			Database_set(conn, id, argv[4], argv[5]);
			Database_write(conn);
			break;
		case 'd':
			if(argc != 4) die("Need an id");
			Database_delete(conn,id);
			Database_write(conn);
			break;
		case 'l':
			Database_list(conn);
			break;
		default:
			die("Invalid action");
			break;
	}
	
	Database_close(conn);
	return 0;
}
开发者ID:miolfo,项目名称:c-the-hard-way,代码行数:92,代码来源:simple_db.c

示例13: Database_create

void Database_create(struct Connection *conn)
{
    int i = 0;

    for(i = 0; i < MAX_ROWS; i++) {
        // make a prototype to initialize it
        struct Address addr = {.id = i, .set = 0};
        // then just assign it
        conn->db->rows[i] = addr;
    }
}

void Database_set(struct Connection *conn, int id, const char *name, const char *email)
{
    struct Address *addr = &conn->db->rows[id];
    if(addr->set) die("Already set, delete it first");
/* &conn->db->rows[i]
    reads "get the i element of rows, which is in db, which is in conn, then get the address of (&) it */


    addr->set = 1;
    // WARNING: bug, read the "How To Break It" and fix this
    char *res = strncpy(addr->name, name, MAX_DATA);
    // demonstrate the strncpy bug
    if(!res) die("Name copy failed");

    res = strncpy(addr->email, email, MAX_DATA);
    if(!res) die("Email copy failed");
}

void Database_get(struct Connection *conn, int id)
{
    struct Address *addr = &conn->db->rows[id];

    if(addr->set) {
        Address_print(addr);
    } else {
        die("ID is not set");
    }
}

void Database_delete(struct Connection *conn, int id)
{
    struct Address addr = {.id = id, .set = 0};
    conn->db->rows[id] = addr;
//  This trick makes sure that all fields but set and id are initialized to 0s
        // and is actually easier to write.
// The dot before the fieldname is for giving initial values to struct members at declaration
}

void Database_list(struct Connection *conn)
{
    int i = 0;
    struct Database *db = conn->db;

    for(i = 0; i < MAX_ROWS; i++) {
        struct Address *cur = &db->rows[i];

        if(cur->set) {
            Address_print(cur);
        }
    }
}

int main(int argc, char *argv[])
{
    if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");

    char *filename = argv[1];
    char action = argv[2][0];
    struct Connection *conn = Database_open(filename, action);
    int id = 0;

//  The atoi is to take the string for the id on the command line and convert it to the int id variable
    if(argc > 3) id = atoi(argv[3]);
    if(id >= MAX_ROWS) die("There's not that many records.");

    switch(action) {
        case 'c':
            Database_create(conn);
            Database_write(conn);
            break;

        case 'g':
            if(argc != 4) die("Need an id to get");

            Database_get(conn, id);
            break;

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

            Database_set(conn, id, argv[4], argv[5]);
            Database_write(conn);
            break;

        case 'd':
            if(argc != 4) die("Need id to delete");

            Database_delete(conn, id);
//.........这里部分代码省略.........
开发者ID:timerg,项目名称:C_Learn,代码行数:101,代码来源:ex17_db.c

示例14: Database_create

void Database_create(struct Connection *conn) {
  int i = 0;

  for(i=0; i < MAX_ROWS; i++) {
    struct Contact c = { .id = i, .set = 0 };
    conn->db->rows[i] = c;
  }
}

void Database_set(struct Connection *conn, int id, const char *name, const char *email) {
  struct Contact *c = &conn->db->rows[id]; // TODO: what does the & do to a struct pointer?
  if(c->set) die("Contact already set, delete it first");

  c->set = 1;
  // WARNING: bug, read the "How To Break It" and fix this
  char *res = strncpy(c->name, name, MAX_DATA);
  if(!res) die("Name copy failed");

  res = strncpy(c->email, email, MAX_DATA);
  if(!res) die("Email copy failed");
}

void Database_get(struct Connection *conn, int id) {
  struct Contact *c = &conn->db->rows[id];
  if(c->set) {
    Contact_print(c);
  } else {
    die("ID not set");
  }
}

void Database_delete(struct Connection *conn, int id) {
  struct Contact c = { .id = id, .set = 0 };
  conn->db->rows[id] = c;
}

void Database_list(struct Connection *conn) {
  int i = 0;
  struct Database *db = conn->db;

  for(i = 0; i < MAX_ROWS; i++) {
    struct Contact *cursor = &db->rows[i];

    if(cursor->set) {
      Contact_print(cursor);
    }
  }
}

int main(int argc, char *argv[]) {
  if(argc < 3) die("USAGE: ex17 <dbfile> <action> [action params]");

  char *filename = argv[1];
  char action = argv[2][0];

  struct Connection *conn = Database_open(filename, action);
  int id = 0;

  if(argc > 3) id = atoi(argv[3]);
  if(id >= MAX_ROWS) die("There's not that many records.");

  switch(action) {
    case 'c':
      Database_create(conn);
      Database_write(conn);
      break;
    case 'g':
      if(argc != 4) die("Need an id to get");
      Database_get(conn, id);
      break;
    case 's':
      if(argc != 6) die("Need id, name, email to set");
      Database_set(conn, id, argv[4], argv[5]);
      Database_write(conn);
      break;
    case 'd':
      if(argc != 4) die("Need id to delete");
      Database_delete(conn, id);
      Database_write(conn);
      break;
    case 'l':
      Database_list(conn);
      break;
    default:
      die("Invalid action, only: c=create, g=get, s=set, d=del, l=list");
  }

  Database_close(conn);

  return 0;
}
开发者ID:oshea,项目名称:snippets,代码行数:91,代码来源:contact_db.c

示例15: Database_create

void Database_create(struct Connection *conn) {
    int i = 0;

    for (i = 0; i < MAX_ROWS; i++) {
        // make a prototype to initialize it
        struct Address addr = {.id = i, .is_set = false};
        // then just assign it
        conn->db->rows[i] = addr;
    }
}

void Database_set(struct Connection *conn, int id, const char *name, const char *email) {
    struct Address *addr = &conn->db->rows[id];
    if (addr->is_set) {
        die(conn, "Already set, delete it first");
    }
    addr->is_set = true;

    // WARNING: bug, read the "How To Break It" and fix this
    // demonstrate the strncpy bug (Not an undocumented 'bug', but maybe poor design)
    // if name is as long or longer than MAX_DATA, strncpy won't set the last character null.
    // http://randomascii.wordpress.com/2013/04/03/stop-using-strncpy-already/
    // http://stackoverflow.com/questions/14950241/pointer-to-one-struct-in-another-writing-and-reading-it-from-file-gives-segfaul
    char *res = strncpy(addr->name, name, MAX_DATA);
    // Ensure last character is null.
    addr->name[sizeof(addr->name) - 1] = '\0';

    if (!res) {
        die(conn, "Name copy failed");
    }

    res = strncpy(addr->email, email, MAX_DATA);
    // Ensure last character is null.
    addr->email[sizeof(addr->email) - 1] = '\0';
    if (!res) {
        die(conn, "Email copy failed");
    }
}

void Database_get(struct Connection *conn, int id) {
    struct Address *addr = &conn->db->rows[id];

    if (addr->is_set) {
        Address_print(addr);
    } else {
        die(conn, "ID is not set");
    }
}

void Database_delete(struct Connection *conn, int id) {
    // set id and is_set
    // don't set name or email, this initializes them to zero.
    struct Address addr = {.id = id, .is_set = false};
    conn->db->rows[id] = addr;
}

void Database_list(struct Connection *conn) {
    int i = 0;
    struct Database *db = conn->db;

    for (i = 0; i < MAX_ROWS; i++) {
        struct Address *cur = &db->rows[i];

        if (cur->is_set) {
            Address_print(cur);
        }
    }
}

int main(int argc, char *argv[]) {
    if (argc < 3) {
        die(NULL, "USAGE: ex17 <dbfile> <action> [action params]");
    }

    char *filename = argv[1];
    char action = argv[2][0];
    struct Connection *conn = Database_open(filename, action);
    int id = 0;

    if (argc > 3) {
        // atoi converts string to integer
        id = atoi(argv[3]);
    }
    if (id >= MAX_ROWS) {
        die(conn, "There's not that many records.");
    }

    switch (action) {
        case 'c':
            Database_create(conn);
            Database_write(conn);
            break;

        case 'g':
            if (argc != 4) {
                die(conn, "Need an id to get");
            }
            Database_get(conn, id);
            break;

//.........这里部分代码省略.........
开发者ID:beepscore,项目名称:LearnCTheHardWay,代码行数:101,代码来源:ex17.c


注:本文中的Database_list函数示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。