用C链表,任何成员的错误 [英] Linked list in C, no member error
本文介绍了用C链表,任何成员的错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图写一个类的数据库中,我从一个文件中读取所有的键值(格式化keyNULL BYTEvalueNULL BYTE等)。我AMT rying使用链表这一点,但我得到的结构有没有下一个值的误差。请帮帮我!
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;
#包括LT&;&ASSERT.H GT;
#包括LT&;&string.h中GT;
#包括LT&;&stdbool.h GT;#包括sdbm.h静态文件*分贝;
静态布尔开= FALSE;
静态INT ERR = 0,KEYLEN = 8;typedef结构{
字符*名称;
*键旁边;
}关键;静态密钥*头= NULL,*尾= NULL;
/ **
*创建给定名称的新数据库。你还有
*以sdbm_open()数据库来访问它。返回true
*成功,虚假的失败。
* /
布尔sdbm_create(为const char *名称){//错误:1)的fopen失败2)FCLOSE失败的新的数据库
DB =的fopen(姓名,W);
如果(DB == NULL){
的printf(无法创建文件%s \\ n,名);
ERR = 1;
返回false;
}
如果(FCLOSE(DB)== EOF){
的printf(无法关闭创建文件%s \\ n,名);
ERR = 2;
返回false;
}
返回true;
}
/ **
*打开现有数据库的名字。返回true上
*成功的,虚假的失败。
* /
布尔sdbm_open(为const char *名称){//错误:3)无法打开数据库
DB = FOPEN(姓名,R +);
如果(DB == NULL){
ERR = 3;
的printf(无法打开数据库文件%s \\ n,名);
返回false;
}
打开= TRUE;
INT℃;
布尔INKEY = TRUE;
INT currKey [MAX_KEY_LENGTH]
而((C = GETC(DB))!= EOF){
如果(INKEY&安培;!和C =='\\ 0'){
INKEY = TRUE;
}
否则,如果(INKEY&安培;和C =='\\ 0'){
如果(尾巴!= NULL){
tail->接下来=的malloc(sizeof运算(关键));
尾= tail->接下来,
}
其他{
尾=的malloc(sizeof运算(关键));
头=尾;
}
tail->接着= NULL;
tail->名称= currKey; }
否则,如果(INKEY){
currKey [KEYLEN] = C;
KEYLEN ++;
}
}
}
/ **
*同步数据库中的所有更改(如果有的话)的磁盘。
*有用的,如果执行缓存中间结果
*在内存中,而不是其直接写入磁盘。
*成功返回真,假失败。
* /
//布尔sdbm_sync();/ **
*关闭数据库,同步更改(如果有的话)。返回
*真正的成功,失败假的。
* /
布尔sdbm_close(){//错误:5)无法关闭数据库
如果(FCLOSE(DB)== EOF){
ERR = 5;
的printf(无法关闭数据库。\\ n);
返回false;
}
返回true;
}
/ **
*返回错误code,去年失败的数据库操作。
* /
INT sdbm_error(){
返回ERR;
}/ **
*鉴于是数据库中的关键?
* /
//布尔sdbm_has(为const char *键);/ **
*获取与数据库中的给定键关联的值。
*成功返回真,假失败。
*
* precondition:sdbm_has(键)
* /
//布尔sdbm_get(为const char *键,字符*值);/ **
在数据库定键关联*更新的价值
*给定值。成功返回true,假
*失败。
*
* precondition:sdbm_has(键)
* /
//布尔sdbm_put(为const char *键,为const char *值);/ **
*鉴于键和值到数据库作为新的插入
*关联。成功返回true,假
*失败。
*
* precondition:sdbm_has(键)
* /
//布尔sdbm_insert(为const char *键,为const char *值);/ **
*给删除键,然后从数据库相关联的价值。
*成功返回真,假失败。
*
* precondition:sdbm_has(键)
* /
//布尔sdbm_remove(为const char *键);
解决方案
的定义应该是这样
typedef结构键{
字符*名称;
结构主要*接下来的; //< - 注意这里`struct`
}键;
您不能只使用键
[无才明确写入结构]结构的定义里面,因为结构尚未确定[typedef操作]
I am trying to write a database for class in which I read all the key values in from a file (formatted keyNULL BYTEvalueNULL BYTE etc). I amt rying to use a linked list for this, but I get the error that the struct has no next value. Please help me!
#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 );
解决方案
The definition should be like
typedef struct Key{
char *name;
struct Key *next; // <-- Note `struct` here
}Key;
You cannot use just Key
[without explicitly writing struct before it] inside the definition of the structure because the structure is not yet defined [typedefed]
这篇关于用C链表,任何成员的错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文