c - C中的链接列表,无成员错误

标签 c linked-list

我正在尝试为类编写数据库,在该数据库中,我从文件(格式为keyNULL BYTEvalueNULL BYTE等)中读取所有键值。我不愿意为此使用链接列表,但是却收到该结构没有下一个值的错误。请帮我!

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

#include "sdbm.h"

static FILE *db;
static bool opened = false;
static int err = 0, keyLen = 8;

typedef struct {
  char *name;
  Key *next;
} Key;

static Key *head = NULL,*tail = 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.
 */
bool sdbm_create( const char *name ) { //Errors: 1) fopen failed 2) fclose failed on new db
  db = fopen(name, "w");
  if (db == NULL) {
    printf("Couldn't create file %s\n",name);
    err = 1;
    return false;
  }
  if (fclose(db) == 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;
  int currKey[MAX_KEY_LENGTH];
  while ((c = getc(db)) != EOF)  {
    if (!inKey && c == '\0') {
      inKey = true;
    }
    else if (inKey && c == '\0') {
      if (tail != NULL) {
        tail->next = malloc(sizeof(Key));
        tail = tail->next;
      }
      else {
        tail = malloc(sizeof(Key));
        head = tail;
      }
      tail->next = NULL;
      tail->name = currKey;

    }
    else if (inKey) { 
      currKey[keyLen] = c;
      keyLen++;
    }
  }
}
/**
 * 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();

/**
 * Close database, synchronizing changes (if any). Return
 * true on success, false on failure.
 */
bool sdbm_close() { // Errors: 5) Couldn't close database
  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 );

/**
 * 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 );

/**
 * 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 );

/**
 * 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 );

/**
 * 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 );

最佳答案

定义应该像

typedef struct Key{
  char *name;
  struct Key *next; // <-- Note `struct` here
}Key;

您不能在结构的定义内仅使用Key [没有在其之前显式写入struct],因为 undefined 结构[typedefed]

关于c - C中的链接列表,无成员错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3869110/

相关文章:

c++ - 使用 ffmpeg 代码的 RTSP 服务器有错误的 ffmpeg 版本?

c - 我将如何使用 Ncurses 在屏幕周围制作一个框

javascript - 避免 JS 反向单链表中的竞争条件

c - C和MATLAB中的矩阵乘法,结果不尽相同

c - 当内核需要紧急释放内存时,JBD2中的检查点如何管理?

c - C 中的返回值

c - 广度优先搜索问题

循环链表插入两个后只有一个元素

algorithm - 在内存管理中使用链表的缺点

c++ - c++ if语句中的多个条件(通过链表的堆栈实现)