Click here to Skip to main content
15,890,506 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: what is zed doing here? Pin
jeron15-Aug-14 11:17
jeron15-Aug-14 11:17 
GeneralRe: what is zed doing here? Pin
Alex Sturza5-Aug-14 11:28
Alex Sturza5-Aug-14 11:28 
GeneralRe: what is zed doing here? Pin
jeron15-Aug-14 11:41
jeron15-Aug-14 11:41 
GeneralRe: what is zed doing here? Pin
Alex Sturza5-Aug-14 11:46
Alex Sturza5-Aug-14 11:46 
AnswerRe: what is zed doing here? Pin
Pete O'Hanlon5-Aug-14 12:27
mvePete O'Hanlon5-Aug-14 12:27 
GeneralRe: what is zed doing here? Pin
Alex Sturza6-Aug-14 4:12
Alex Sturza6-Aug-14 4:12 
GeneralRe: what is zed doing here? Pin
Pete O'Hanlon6-Aug-14 5:31
mvePete O'Hanlon6-Aug-14 5:31 
Questionin this code,where is called database_close?pls help Pin
Alex Sturza5-Aug-14 6:19
Alex Sturza5-Aug-14 6:19 
#include <stdio.h>
#include <assert.h>
#include <stdlib.h>
#include <errno.h>
#include <string.h>

#define MAX_DATA 512
#define MAX_ROWS 100

struct Address {
int id;
int set;
char name[MAX_DATA];
char email[MAX_DATA];
};

struct Database {
struct Address rows[MAX_ROWS];
};

struct Connection {
FILE *file;
struct Database *db;
};

void die(const char *message)
{
if(errno) {
perror(message);
} else {
printf("ERROR: %s\n", message);
}

exit(1);
}

void Address_print(struct Address *addr)
{
printf("%d %s %s\n",
addr->id, addr->name, addr->email);
}

void Database_load(struct Connection *conn)
{
int rc = fread(conn->db, sizeof(struct Database), 1, conn->file);
if(rc != 1) die("Failed to load database.");
}

struct Connection *Database_open(const char *filename, char mode)
{
struct Connection *conn = malloc(sizeof(struct Connection));
if(!conn) die("Memory error");

conn->db = malloc(sizeof(struct Database));
if(!conn->db) die("Memory error");

if(mode == 'c') {
conn->file = fopen(filename, "w");
} else {
conn->file = fopen(filename, "r+");

if(conn->file) {
Database_load(conn);
}
}

if(!conn->file) die("Failed to open the file");

return conn;
}

void Database_close(struct Connection *conn)
{
if(conn) {
if(conn->file) fclose(conn->file);
if(conn->db) free(conn->db);
free(conn);
}
}

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.");

rc = fflush(conn->file);
if(rc == -1) die("Cannot flush database.");
}

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");

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;
}

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;

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;
}
QuestionRe: in this code,where is called database_close?pls help Pin
Richard MacCutchan5-Aug-14 6:55
mveRichard MacCutchan5-Aug-14 6:55 
AnswerRe: in this code,where is called database_close?pls help Pin
Alex Sturza5-Aug-14 7:08
Alex Sturza5-Aug-14 7:08 
SuggestionRe: in this code,where is called database_close?pls help Pin
Richard MacCutchan5-Aug-14 7:41
mveRichard MacCutchan5-Aug-14 7:41 
AnswerRe: in this code,where is called database_close?pls help Pin
David Crow5-Aug-14 6:58
David Crow5-Aug-14 6:58 
GeneralRe: in this code,where is called database_close?pls help Pin
Alex Sturza5-Aug-14 7:08
Alex Sturza5-Aug-14 7:08 
QuestionConvert WMI Win32_OperatingSystem InstallDate to mm/dd/yyyy format. Pin
mbatra315-Aug-14 1:56
mbatra315-Aug-14 1:56 
AnswerRe: Convert WMI Win32_OperatingSystem InstallDate to mm/dd/yyyy format. Pin
«_Superman_»5-Aug-14 2:09
professional«_Superman_»5-Aug-14 2:09 
Question'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
Swap95-Aug-14 1:33
Swap95-Aug-14 1:33 
AnswerRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
«_Superman_»5-Aug-14 2:04
professional«_Superman_»5-Aug-14 2:04 
GeneralRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
Swap95-Aug-14 3:43
Swap95-Aug-14 3:43 
GeneralRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
Swap96-Aug-14 1:31
Swap96-Aug-14 1:31 
GeneralRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
«_Superman_»6-Aug-14 20:22
professional«_Superman_»6-Aug-14 20:22 
AnswerRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
CPallini5-Aug-14 2:36
mveCPallini5-Aug-14 2:36 
GeneralRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
Swap95-Aug-14 3:50
Swap95-Aug-14 3:50 
GeneralRe: 'sprintf': cannot convert parameter 1 from 'TCHAR *[1024]' to 'char *' Pin
Richard MacCutchan5-Aug-14 5:32
mveRichard MacCutchan5-Aug-14 5:32 
Questionhow to correct this warning? Pin
mybm15-Aug-14 0:02
mybm15-Aug-14 0:02 
AnswerRe: how to correct this warning? Pin
Richard MacCutchan5-Aug-14 0:08
mveRichard MacCutchan5-Aug-14 0:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.