本文整理汇总了C++中Database_create函数的典型用法代码示例。如果您正苦于以下问题:C++ Database_create函数的具体用法?C++ Database_create怎么用?C++ Database_create使用的例子?那么恭喜您, 这里精选的函数代码示例或许可以为您提供帮助。
在下文中一共展示了Database_create函数的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C++代码示例。
示例1: 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;
}
示例2: Database_load
// Declare the Database_load function
void Database_load()
{
rewind(conn->file);
// Read conn->file into conn->db
// Each block is the sizeof(struct Database)
// Read 1 block
int rc = fread(&conn->db->max_rows, sizeof(int), 1, conn->file);
if (rc != 1) die("Failed to load database.");
rc = fread(&conn->db->max_data, sizeof(int), 1, conn->file);
if (rc != 1) die("Failed to load database.");
if (conn->db->rows == NULL) {
Database_create(conn->db->max_rows, conn->db->max_data);
}
int i = 0;
for (i = 0; i < conn->db->max_rows; i++) {
struct Address *row = &conn->db->rows[i];
fread(&row->id, sizeof(int), 1, conn->file);
fread(&row->set, sizeof(int), 1, conn->file);
fread(row->name, sizeof(char) * conn->db->max_data, 1, conn->file);
fread(row->email, sizeof(char) * conn->db->max_data, 1, conn->file);
fread(row->location, sizeof(char) * conn->db->max_data, 1, conn->file);
}
}
示例3: 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;
}
示例4: 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;
}
示例5: 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;
}
示例6: 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;
}
示例7: 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;
}
示例8: 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;
}
示例9: 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;
}
示例10: 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;
}
示例11: Database_create
/*
* === FUNCTION ======================================================================
* Name: Database_create
* Description:
* =====================================================================================
*/
void
Database_create ( struct Connection*conn )
{
int i=0;
for(i=0;i<MAX_ROWS;i++){
struct Address addr = { .id=i, .set=0 };
conn->db->rows[i] = addr;
}
} /* ----- end of function Database_create ----- */
/*
* === FUNCTION ======================================================================
* Name: Database_set
* Description:
* =====================================================================================
*/
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");
addr->set = 1;
char *res = strncpy(addr->name, name, MAX_DATA);
if(strlen(addr->name) >= MAX_DATA)
printf("WARNING:length_error\n");
res[MAX_DATA-1] = '\0';
if(!res) die("Name copy failed");
printf("name is %s\n", addr->name);
char* eres = strncpy(addr->email, email, MAX_DATA);
if(!eres) die("Email copy failed");
if(strlen(addr->email) >= MAX_DATA)
printf("WARNING:length_error\n");
eres[MAX_DATA-1] ='\0';
printf("email is %s\n", addr->email);
} /* ----- end of function Database_set ----- */
/*
* === FUNCTION ======================================================================
* Name: Database_get
* Description:
* =====================================================================================
*/
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");
}
} /* ----- end of function Database_get ----- */
/*
* === FUNCTION ======================================================================
* Name: Database_delete
* Description:
* =====================================================================================
*/
void
Database_delete ( struct Connection *conn, int id )
{
struct Address addr = {.id = id, .set=0};
conn->db->rows[id] = addr;
} /* ----- end of function Database_delete ----- */
/*
* === FUNCTION ======================================================================
* Name: Database_list
* Description:
* =====================================================================================
*/
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);
}
}
} /* ----- end of function Database_list ----- */
//.........这里部分代码省略.........
示例12: Database_create
void Database_create(struct Connection *conn) {
int i = 0;
for (i = 0; i < MAX_ROWS; i++) {
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("Record already set - delete first", conn);
}
addr->set = 1;
char *res = strncpy(addr->name, name, MAX_DATA);
if (!res) {
die("name copy failed", conn);
}
res = strncpy(addr->email, email, MAX_DATA);
if (!res) {
die("email copy failed", conn);
}
}
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", conn);
}
}
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 *addr = &db->rows[i];
if (addr->set) {
Address_print(addr);
printf("\n");
}
}
}
void die(const char *message, struct Connection *conn) {
if (errno) {
perror(message);
} else {
printf("ERROR %s\n", message);
}
Database_close(conn);
exit(1);
}
int main(int argc, char *argv[]) {
if (argc < 3) {
die("USAGE: simple-database <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) {
id = atoi(argv[3]);
}
if (id > MAX_ROWS) {
die("ID exceeds maximum database capacity", conn);
}
switch(action) {
case 'c':
Database_create(conn);
Database_write(conn);
break;
case 'g':
if (argc != 4) {
die("An ID is required for the get operation", conn);
}
Database_get(conn, id);
break;
case 's':
if (argc != 6) {
die("Need an ID, name, and email for the set operation", conn);
}
//.........这里部分代码省略.........
示例13: main
//Database handler. UI
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 != 'f'){
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 != 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;
case 'f':
if(argc!=4)
die("Need name to look for.", conn);
Database_find(conn, argv[3]);
break;
default:
die("Invalid action. Only c=create, g=get, s=set, d=delete and l=list are valid actions.", conn);
}
Database_close(conn);
return 0;
}
示例14: 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;
}
示例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, .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);
//.........这里部分代码省略.........