C 自定义数据库写入错误

发布于 2024-09-26 19:13:50 字数 6956 浏览 0 评论 0原文

我有一个班级作业,我必须编写一个程序来读取键、值对并将其写入磁盘。我使用链接列表来存储键,并在需要时从磁盘读取值。但是,我在更改和删除值时遇到问题。我正在用它来测试它: http: //gaming.jhu.edu/~phf/2010/fall/cs120/src/sdbm-examples.tar.gz。代码如下。基本上,我需要一些帮助来找出错误,因为这是我们必须使用指针的第一个作业,而我只是在所有的段错误和其他一切中死去。只是一些建议将不胜感激。

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

#include "sdbm.h"

FILE *db;
bool opened = false, needNewDB = false;
int err = 0, keyLen = 0;
char *filename;

typedef struct Key_{
  char *name;
  char *val;
  long offset;
  struct Key_ *next;
} Key;

Key *head = NULL,*tail = NULL, *lastHas = NULL, *beforeLastHas = NULL;
/**
 * Create new database with given name. You still have
 * to sdbm_open() the database to access it. Return true
 * on success, false on failure.
 */
void listAdd() {
  if (tail != NULL) {
    tail->next = (Key *) malloc(sizeof(Key));
    tail = tail->next;
  }
  else {
    tail = (Key *)malloc(sizeof(Key));
    head = tail;
  }
  tail->next = NULL;
  tail->name = NULL;
  tail->val = NULL;
}

bool sdbm_create( const char *name ) { //Errors: 1) fopen failed 2) fclose failed on new db
  filename = malloc(sizeof(*name));
  strcpy(filename,name);
  FILE *temp = fopen(name, "w");
  if (temp == NULL) {
    printf("Couldn't create file %s\n",name);
    err = 1;
    return false;
  }
  if (fclose(temp) == EOF) {
    printf("Couldn't close created file %s\n",name);
    err = 2;
    return false;
  }
  return true;
}
/**
 * Open existing database with given name. Return true on
 * success, false on failure.
 */
bool sdbm_open( const char *name ) { //Errors: 3) couldn't open database
  db = fopen(name,"r+");
  if (db == NULL) {
    err = 3;
    printf("Couldn't open database file %s\n",name);
    return false;
  }
  opened = true;
  int c;
  bool inKey = true;
  char currKey[MAX_KEY_LENGTH];
  while ((c = getc(db)) != EOF)  {
    if (!inKey && c == '\0') {
      inKey = true;
    }
    else if (inKey && c == '\0') {
      currKey[keyLen] = '\0';
      listAdd();
      tail->offset = ftell(db);
      tail->name = malloc(sizeof(*currKey));
      strcpy(tail->name,currKey);
      keyLen = 0;
      inKey = false;
    }
    else if (inKey) { 
      currKey[keyLen] = c;
      keyLen++;
    }
  }
  Key *curr = head;
  while (curr != NULL) {
    printf("Key: %s\n",curr->name);
    curr = curr->next;
  }

  return true;
}
void readVal(char *value, long offset) {
  fseek(db,offset,SEEK_SET);
  int c;
  for (int i = 0; (c = getc(db)) != '\0'; i++) {
    *(value + i) = c;
  }
}

/**
 * Synchronize all changes in database (if any) to disk.
 * Useful if implementation caches intermediate results
 * in memory instead of writing them to disk directly.
 * Return true on success, false on failure.
 */
bool sdbm_sync() {
  if (!needNewDB) {
    Key *curr = head;
    fseek(db,0,SEEK_END);
    while (curr != NULL) {
      if (curr->val != NULL) {
        fprintf(db,"%s%c%s%c",curr->name,'\0',curr->val,'\0');
      }
      curr = curr->next;
    }
  }
  else {
    FILE *temp;
    sdbm_create("tRpdxD.p4ed");
    temp = fopen("tRpdxD.p4ed","w");
    Key *curr = head;
    while (curr != NULL) {
      if (curr->val != NULL) {
        fprintf(temp,"%s%c%s%c",curr->name, '\0', curr->val, '\0');
      }else {
        char *tempS = malloc(MAX_VALUE_LENGTH);
        readVal(tempS, curr->offset);
        fprintf(temp,"%s%c%s%c",curr->name,'\0',tempS,'\0');
        free(tempS);
      }
      fflush(temp);
      fflush(db);
      curr = curr->next;
    }
    fclose(db);
    remove(filename);
    rename("tRpdxD.p4ed",filename);
    db = fopen(filename,"r+");
  }
  fflush(db);
  return true;
}
/**
 * Close database, synchronizing changes (if any). Return
 * true on success, false on failure.
 */
bool sdbm_close() { // Errors: 5) Couldn't close database
  sdbm_sync();
  Key *tmp = head;
  while (head->next != NULL) {
    tmp = head;
    head = head->next;
    free(tmp->name);
    if (tmp->val != NULL) {
      free(tmp->val);
    }
    free(tmp);
  }
  if (fclose(db) == EOF) {
    err = 5;
    printf("Couldn't close database.\n");
    return false;
  }
  return true;
}
/**
 * Return error code for last failed database operation.
 */
int sdbm_error() {
  return err;
}

/**
 * Is given key in database?
 */
bool sdbm_has( const char *key ) {
  if (head == NULL) {
    return false;
  }
  Key *curr = head;
  lastHas = NULL;
  beforeLastHas = NULL;
  while (curr != NULL) {
    if (!strcmp(curr->name,key)) {

      lastHas = curr;
      return true;
    }
    beforeLastHas = curr;
    curr = curr->next;
  }
  return false;
}

/**
 * Get value associated with given key in database.
 * Return true on success, false on failure.
 *
 * Precondition: sdbm_has(key)
 */
bool sdbm_get( const char *key, char *value ) { //Errors: 6)Don't have key
  if (!sdbm_has(key)) {
    printf("Precondition sdbm_has(%s) failed", key);
    err = 6;
    return false;
  }
  readVal(value, lastHas->offset);
  return true;
}

/**
 * Update value associated with given key in database
 * to given value. Return true on success, false on
 * failure.
 *
 * Precondition: sdbm_has(key)
 */
bool sdbm_put( const char *key, const char *value ) {
  if (!sdbm_has(key)) {
    printf("Precondition !sdbm_has(%s) failed",key);
    err = 7;
    return false;
  }
  sdbm_remove(key);
  sdbm_insert(key,value);
  return true;
}
/**
 * Insert given key and value into database as a new
 * association. Return true on success, false on
 * failure.
 *
 * Precondition: !sdbm_has(key)
 */
bool sdbm_insert( const char *key, const char *value ) { //Errors: 7)Already have key 8)Invalid key or value length
  if (sdbm_has(key)) {
    printf("Precondition !sdbm_has(%s) failed",key);
    err = 7;
    return false;
  }
  if (strlen(key) < MIN_KEY_LENGTH || strlen(key) > MAX_KEY_LENGTH || strlen(value) < MIN_VALUE_LENGTH || strlen(value) > MAX_VALUE_LENGTH) {
    printf("Invalid key or value length");
    err = 8;
    return false;
  }
  listAdd();
  tail->name = (char *)key;
  tail->val = malloc(sizeof(*value));
  strcpy(tail->val,value);
  return true;
}

/**
 * Remove given key and associated value from database.
 * Return true on success, false on failure.
 *
 * Precondition: sdbm_has(key)
 */
bool sdbm_remove( const char *key ) {
  if (!sdbm_has(key)) {
    printf("Precondition !sdbm_has(%s) failed",key);
    err = 7;
    return false;
  }
  needNewDB = true;
  if (beforeLastHas == NULL) {
    head = lastHas->next;
  }
  else if (lastHas->next == NULL) {
    tail = beforeLastHas;
  }
  else {
    beforeLastHas->next = lastHas->next;
  }
  if (lastHas->val != NULL) {
    free(lastHas->val);
  }
  free(lastHas->name);
  free(lastHas);
  return true;
}

I have an assignment for class that I have to write a program to read and write key, value pairs to disk. I am using a linked list to store the keys, and read in values whenever I need to from disk. However, I am having trouble changing and deleting values. I am using this to test it: http://gaming.jhu.edu/~phf/2010/fall/cs120/src/sdbm-examples.tar.gz. Code below. Basically, I need some help figuring out errors, because this is the first assignment we have had to use pointers on, and I am just dying in all the segfaults and everything else. Just some advice would be greatly appreciated.

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <string.h>
#include <stdbool.h>

#include "sdbm.h"

FILE *db;
bool opened = false, needNewDB = false;
int err = 0, keyLen = 0;
char *filename;

typedef struct Key_{
  char *name;
  char *val;
  long offset;
  struct Key_ *next;
} Key;

Key *head = NULL,*tail = NULL, *lastHas = NULL, *beforeLastHas = NULL;
/**
 * Create new database with given name. You still have
 * to sdbm_open() the database to access it. Return true
 * on success, false on failure.
 */
void listAdd() {
  if (tail != NULL) {
    tail->next = (Key *) malloc(sizeof(Key));
    tail = tail->next;
  }
  else {
    tail = (Key *)malloc(sizeof(Key));
    head = tail;
  }
  tail->next = NULL;
  tail->name = NULL;
  tail->val = NULL;
}

bool sdbm_create( const char *name ) { //Errors: 1) fopen failed 2) fclose failed on new db
  filename = malloc(sizeof(*name));
  strcpy(filename,name);
  FILE *temp = fopen(name, "w");
  if (temp == NULL) {
    printf("Couldn't create file %s\n",name);
    err = 1;
    return false;
  }
  if (fclose(temp) == EOF) {
    printf("Couldn't close created file %s\n",name);
    err = 2;
    return false;
  }
  return true;
}
/**
 * Open existing database with given name. Return true on
 * success, false on failure.
 */
bool sdbm_open( const char *name ) { //Errors: 3) couldn't open database
  db = fopen(name,"r+");
  if (db == NULL) {
    err = 3;
    printf("Couldn't open database file %s\n",name);
    return false;
  }
  opened = true;
  int c;
  bool inKey = true;
  char currKey[MAX_KEY_LENGTH];
  while ((c = getc(db)) != EOF)  {
    if (!inKey && c == '\0') {
      inKey = true;
    }
    else if (inKey && c == '\0') {
      currKey[keyLen] = '\0';
      listAdd();
      tail->offset = ftell(db);
      tail->name = malloc(sizeof(*currKey));
      strcpy(tail->name,currKey);
      keyLen = 0;
      inKey = false;
    }
    else if (inKey) { 
      currKey[keyLen] = c;
      keyLen++;
    }
  }
  Key *curr = head;
  while (curr != NULL) {
    printf("Key: %s\n",curr->name);
    curr = curr->next;
  }

  return true;
}
void readVal(char *value, long offset) {
  fseek(db,offset,SEEK_SET);
  int c;
  for (int i = 0; (c = getc(db)) != '\0'; i++) {
    *(value + i) = c;
  }
}

/**
 * Synchronize all changes in database (if any) to disk.
 * Useful if implementation caches intermediate results
 * in memory instead of writing them to disk directly.
 * Return true on success, false on failure.
 */
bool sdbm_sync() {
  if (!needNewDB) {
    Key *curr = head;
    fseek(db,0,SEEK_END);
    while (curr != NULL) {
      if (curr->val != NULL) {
        fprintf(db,"%s%c%s%c",curr->name,'\0',curr->val,'\0');
      }
      curr = curr->next;
    }
  }
  else {
    FILE *temp;
    sdbm_create("tRpdxD.p4ed");
    temp = fopen("tRpdxD.p4ed","w");
    Key *curr = head;
    while (curr != NULL) {
      if (curr->val != NULL) {
        fprintf(temp,"%s%c%s%c",curr->name, '\0', curr->val, '\0');
      }else {
        char *tempS = malloc(MAX_VALUE_LENGTH);
        readVal(tempS, curr->offset);
        fprintf(temp,"%s%c%s%c",curr->name,'\0',tempS,'\0');
        free(tempS);
      }
      fflush(temp);
      fflush(db);
      curr = curr->next;
    }
    fclose(db);
    remove(filename);
    rename("tRpdxD.p4ed",filename);
    db = fopen(filename,"r+");
  }
  fflush(db);
  return true;
}
/**
 * Close database, synchronizing changes (if any). Return
 * true on success, false on failure.
 */
bool sdbm_close() { // Errors: 5) Couldn't close database
  sdbm_sync();
  Key *tmp = head;
  while (head->next != NULL) {
    tmp = head;
    head = head->next;
    free(tmp->name);
    if (tmp->val != NULL) {
      free(tmp->val);
    }
    free(tmp);
  }
  if (fclose(db) == EOF) {
    err = 5;
    printf("Couldn't close database.\n");
    return false;
  }
  return true;
}
/**
 * Return error code for last failed database operation.
 */
int sdbm_error() {
  return err;
}

/**
 * Is given key in database?
 */
bool sdbm_has( const char *key ) {
  if (head == NULL) {
    return false;
  }
  Key *curr = head;
  lastHas = NULL;
  beforeLastHas = NULL;
  while (curr != NULL) {
    if (!strcmp(curr->name,key)) {

      lastHas = curr;
      return true;
    }
    beforeLastHas = curr;
    curr = curr->next;
  }
  return false;
}

/**
 * Get value associated with given key in database.
 * Return true on success, false on failure.
 *
 * Precondition: sdbm_has(key)
 */
bool sdbm_get( const char *key, char *value ) { //Errors: 6)Don't have key
  if (!sdbm_has(key)) {
    printf("Precondition sdbm_has(%s) failed", key);
    err = 6;
    return false;
  }
  readVal(value, lastHas->offset);
  return true;
}

/**
 * Update value associated with given key in database
 * to given value. Return true on success, false on
 * failure.
 *
 * Precondition: sdbm_has(key)
 */
bool sdbm_put( const char *key, const char *value ) {
  if (!sdbm_has(key)) {
    printf("Precondition !sdbm_has(%s) failed",key);
    err = 7;
    return false;
  }
  sdbm_remove(key);
  sdbm_insert(key,value);
  return true;
}
/**
 * Insert given key and value into database as a new
 * association. Return true on success, false on
 * failure.
 *
 * Precondition: !sdbm_has(key)
 */
bool sdbm_insert( const char *key, const char *value ) { //Errors: 7)Already have key 8)Invalid key or value length
  if (sdbm_has(key)) {
    printf("Precondition !sdbm_has(%s) failed",key);
    err = 7;
    return false;
  }
  if (strlen(key) < MIN_KEY_LENGTH || strlen(key) > MAX_KEY_LENGTH || strlen(value) < MIN_VALUE_LENGTH || strlen(value) > MAX_VALUE_LENGTH) {
    printf("Invalid key or value length");
    err = 8;
    return false;
  }
  listAdd();
  tail->name = (char *)key;
  tail->val = malloc(sizeof(*value));
  strcpy(tail->val,value);
  return true;
}

/**
 * Remove given key and associated value from database.
 * Return true on success, false on failure.
 *
 * Precondition: sdbm_has(key)
 */
bool sdbm_remove( const char *key ) {
  if (!sdbm_has(key)) {
    printf("Precondition !sdbm_has(%s) failed",key);
    err = 7;
    return false;
  }
  needNewDB = true;
  if (beforeLastHas == NULL) {
    head = lastHas->next;
  }
  else if (lastHas->next == NULL) {
    tail = beforeLastHas;
  }
  else {
    beforeLastHas->next = lastHas->next;
  }
  if (lastHas->val != NULL) {
    free(lastHas->val);
  }
  free(lastHas->name);
  free(lastHas);
  return true;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

梦里兽 2024-10-03 19:13:50

这段代码有很多错误。仅举一个:

filename = malloc(sizeof(*name)); 

*namename 的第一个元素,因此它是一个 char,因此 sizeof(*name) = = 1 。要获取字符串的大小,请使用 strlen(name) + 1。更好的是,如果您的系统使用 strdup有它。

There's a lot of errors in this code. To name just one:

filename = malloc(sizeof(*name)); 

*name is the first element of name, so it's a char, so sizeof(*name) == 1. To get the size of a string, use strlen(name) + 1. Better yet, use strdup if your system has it.

把人绕傻吧 2024-10-03 19:13:50

我建议不要使用全局变量。您无法更改程序(将来)以并行使用两个数据库。

因此,所有 sdbm_xxx 函数都应该自行获取(或推断)所有必要的值。

i would advise against global variables. you can not change the program (in the future) to use two of your databases in parallel.

so all your sdbm_xxx functions should get (or infer) all necessary values on their own.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文