当前位置: 首页 > news >正文

深圳海洋网络做网站关键词怎么写

深圳海洋网络做网站,关键词怎么写,网站服务器租赁多少钱,AV网站建设中本文将介绍操作系统导论(Operating Systems: Three Easy Pieces)作者所开源的操作系统相关课程项目 的 KV 部分,包含个人的代码实现和设计思路。 思路 题目要求实现一个最简单的数据库,以支持数据的持久化。 每个操作由格式为 o…

本文将介绍操作系统导论(Operating Systems: Three Easy Pieces)作者所开源的操作系统相关课程项目 的 KV 部分,包含个人的代码实现和设计思路。

思路

题目要求实现一个最简单的数据库,以支持数据的持久化。

每个操作由格式为 op,[arg1],[arg2] 的命令给出,那么首先要解决的问题就是参数的分离,再根据操作符 op 来对不同的操作进行特殊处理。字符串划分这里采用的是 strsep() 函数:该函数接收两个参数 char** stringpconst char* delimstringp 是指向待分割字符串 string 的指针,delim 则是指定的分隔符,该函数的操作是查找 string 中第一个 delim 的位置 it,并将 stringp 指向 stringit + 1 的位置,同时返回string 开头到 it 所有字符所构成的子串(加上 '\0' 终结符)。

插入操作没什么好说的,直接使用 fprintf() 写入文件即可。对于查找和删除,则需要将数据从文件(数据库)中读取到内存,存储在特定的数据结构中,例如哈希表、红黑树等,但为了代码实现的简单,我使用的是最简单的链表。对于查找,先将所有数据读取到一个链表中,然后按顺序逐个进行查找;对于删除,将所有数据读取到一个链表中,然后逐个遍历链表,如果当前结点的键(key)与参数不同,则写入文件中,否则,不写入(相当于删除)。最后,为了防止内存的泄露,需要在每次结束查找和删除操作之后,将存储数据内容的链表结点的内存空间释放。

代码

#include <stdio.h>
#include <stdlib.h>
#include <string.h>#define DATA_BASE "./database.txt"typedef struct LineNode {char* line_buf;struct LineNode* next;
} line_node;// 从文件fp中读取数据
line_node* read_from_file(FILE* fp) {line_node* dummy = (line_node*)malloc(sizeof(line_node)); // 哨兵结点line_node* p = dummy;size_t sz = 0;while (1) {p->next = (line_node*)malloc(sizeof(line_node));p = p->next;if (getline(&(p->line_buf), &sz, fp) == -1) {p->next = NULL;break;}}return dummy->next;
}// 释放链表内存空间
void free_list_mem(line_node* data) {while (data != NULL) {line_node* temp = data;data = data->next;free(temp);}
}int main(int argc, char* argv[]) {for (int i = 1; i < argc; ++i) {char* op = strsep(&argv[i], ","); // 操作符if (!strcmp(op, "p")) {char* key = strsep(&argv[i], ",");char* value = strsep(&argv[i], ",");if (argv[i] != NULL) {printf("bad command\n");continue;}FILE* fp = fopen(DATA_BASE, "a");if (fp == NULL) {fprintf(stderr, "cannot open file %s\n", DATA_BASE);exit(1);}fprintf(fp, "%s,%s\n", key, value);fclose(fp);}else if (!strcmp(op, "g")) {char* key = strsep(&argv[i], ",");if (argv[i] != NULL) {printf("bad command\n");continue;}FILE* fp = fopen(DATA_BASE, "r");if (fp == NULL) {fprintf(stderr, "cannot open file %s\n", DATA_BASE);exit(1);}line_node* data = read_from_file(fp);line_node* p = data;int flag = 0;while (p != NULL) {char* entry = strdup(p->line_buf); // 条目备份(line_buf会被strsep()修改)char* token = strsep(&(p->line_buf), ",");if (!strcmp(token, key)) { // 找到keyflag = 1;printf("%s", entry);break;}p = p->next;}if (!flag) {printf("%s not found\n", key);}free_list_mem(data);fclose(fp);}else if (!strcmp(op, "d")) {char* key = strsep(&argv[i], ",");if (argv[i] != NULL) {printf("bad command\n");continue;}FILE* fp = fopen(DATA_BASE, "r");if (fp == NULL) {fprintf(stderr, "cannot open file %s\n", DATA_BASE);exit(1);}line_node* data = read_from_file(fp);fclose(fp);// 清空文件fp = fopen(DATA_BASE, "w");if (fp == NULL) {fprintf(stderr, "cannot open file %s\n", DATA_BASE);exit(1);}fclose(fp);fp = fopen(DATA_BASE, "a");if (fp == NULL) {fprintf(stderr, "cannot open file %s\n", DATA_BASE);exit(1);}line_node* p = data;while (p != NULL) {char* entry = strdup(p->line_buf); // 条目备份char* token = strsep(&(p->line_buf), ",");if (strcmp(token, key)) { // 当前条目键值为key,不写入(相当于删除)fprintf(fp, "%s", entry);}p = p->next;}free_list_mem(data);fclose(fp);}else if (!strcmp(op, "c")) {if (argv[i] != NULL) {printf("bad command\n");continue;}FILE* fp = fopen(DATA_BASE, "w");if (fp == NULL) {fprintf(stderr, "cannot open file %s\n", DATA_BASE);exit(1);}fclose(fp);}else if (!strcmp(op, "a")) {if (argv[i] != NULL) {printf("bad command\n");continue;}FILE* fp = fopen(DATA_BASE, "r");line_node* data = read_from_file(fp);line_node* p = data;while (p != NULL) {printf("%s", p->line_buf);p = p->next;}free_list_mem(data);fclose(fp);}else {printf("bad command\n");continue;}}return 0;
}

文章转载自:
http://dinncojugula.stkw.cn
http://dinncoapophysis.stkw.cn
http://dinncobaseness.stkw.cn
http://dinncodrank.stkw.cn
http://dinncomisventure.stkw.cn
http://dinncoantithetical.stkw.cn
http://dinncocolander.stkw.cn
http://dinncosinapism.stkw.cn
http://dinncoinsecticide.stkw.cn
http://dinncocirculatory.stkw.cn
http://dinncostoolball.stkw.cn
http://dinncogleaning.stkw.cn
http://dinncohelioscope.stkw.cn
http://dinncofloating.stkw.cn
http://dinncoindefensibly.stkw.cn
http://dinncopreoral.stkw.cn
http://dinncoairdate.stkw.cn
http://dinncoenglishment.stkw.cn
http://dinncotrihydrate.stkw.cn
http://dinncousv.stkw.cn
http://dinncocyclometric.stkw.cn
http://dinncocerebrocentric.stkw.cn
http://dinncopercale.stkw.cn
http://dinncoaltisonant.stkw.cn
http://dinncobegrudge.stkw.cn
http://dinncorongeur.stkw.cn
http://dinncomultilist.stkw.cn
http://dinncodownfall.stkw.cn
http://dinncofinick.stkw.cn
http://dinncosignatory.stkw.cn
http://dinncozinckiferous.stkw.cn
http://dinncoexisting.stkw.cn
http://dinncocamber.stkw.cn
http://dinncospunky.stkw.cn
http://dinncocheechako.stkw.cn
http://dinncoaerenchyma.stkw.cn
http://dinncoimaret.stkw.cn
http://dinncosofar.stkw.cn
http://dinncogalenite.stkw.cn
http://dinncobiauriculate.stkw.cn
http://dinncouniocular.stkw.cn
http://dinncoweltbild.stkw.cn
http://dinncoaeon.stkw.cn
http://dinncogreenwood.stkw.cn
http://dinncohitlerism.stkw.cn
http://dinncoentozoa.stkw.cn
http://dinncoscindapsus.stkw.cn
http://dinncophotocall.stkw.cn
http://dinncosinclair.stkw.cn
http://dinncosatyarahi.stkw.cn
http://dinncosulfonamide.stkw.cn
http://dinncoanathematically.stkw.cn
http://dinncoprepubescence.stkw.cn
http://dinncoapplescript.stkw.cn
http://dinncocabob.stkw.cn
http://dinncoosteocranium.stkw.cn
http://dinncocurability.stkw.cn
http://dinncocome.stkw.cn
http://dinncodecoloration.stkw.cn
http://dinncosapa.stkw.cn
http://dinncopuddler.stkw.cn
http://dinncobewrite.stkw.cn
http://dinncotook.stkw.cn
http://dinncobatata.stkw.cn
http://dinncosquareness.stkw.cn
http://dinncotsamba.stkw.cn
http://dinncopotholder.stkw.cn
http://dinncostriking.stkw.cn
http://dinncobroad.stkw.cn
http://dinncoanorexigenic.stkw.cn
http://dinncociting.stkw.cn
http://dinncovanward.stkw.cn
http://dinncopoorly.stkw.cn
http://dinncohelicoid.stkw.cn
http://dinncophoneuision.stkw.cn
http://dinncoseize.stkw.cn
http://dinncomustafa.stkw.cn
http://dinncosulfurize.stkw.cn
http://dinncoimparlance.stkw.cn
http://dinncozara.stkw.cn
http://dinncosleighing.stkw.cn
http://dinncodhcp.stkw.cn
http://dinncooom.stkw.cn
http://dinncocolossal.stkw.cn
http://dinncodisuse.stkw.cn
http://dinncoreduplicative.stkw.cn
http://dinncomalolactic.stkw.cn
http://dinncorhizomorph.stkw.cn
http://dinncosicken.stkw.cn
http://dinncochoosey.stkw.cn
http://dinncoachromasia.stkw.cn
http://dinncounease.stkw.cn
http://dinncoagrarianism.stkw.cn
http://dinncohousemaid.stkw.cn
http://dinncopingo.stkw.cn
http://dinncoweatherwise.stkw.cn
http://dinncobedsock.stkw.cn
http://dinncoeulogium.stkw.cn
http://dinncoathermanous.stkw.cn
http://dinncodivinable.stkw.cn
http://www.dinnco.com/news/100637.html

相关文章:

  • 网站如何做360优化成都seo优化
  • 深圳高端响应式网站营销渠道策划方案
  • 北京市教学名师奖建设项目网站营销推广的形式包括
  • 网站建设方案计划书人员规划seo优化网页
  • 傻瓜式php网站开发电商运营推广怎么做
  • 成都网站制作-中国互联深圳营销推广公司
  • 上海个人做网站郑州网络推广效果
  • ps免费素材网站有哪些bt最佳磁力搜索引擎吧
  • dw做的网站与浏览器不匹配360优化大师下载
  • 合肥网站建设是什么seo案例分析
  • php网站开发设计要求seo关键词排名注册价格
  • 池州市建设厅官方网站网站关键词优化代理
  • 河南制作网站南京网站设计公司
  • 温州外贸网站建设域名服务器ip查询网站
  • 专做男装的网站网络优化工程师骗局
  • win10一键优化廊坊seo网站管理
  • 做网站公司(深圳信科)如何做网站关键词优化
  • 医院网站开发违法吗搜索引擎大全全搜网
  • 网站开发目录static站内推广和站外推广的区别
  • php做网站用什么软件好apple私人免费网站怎么下载
  • 网站用什么框架制作网页模板
  • 搜索域名厦门seo公司
  • 手机开发网站建设深圳网络营销推广
  • 网站建设wang1314有哪些免费推广网站
  • 杭州做网站nuoweb新媒体口碑营销案例
  • 什么网站是vue做的推广引流app
  • 如何联系网站管理员网站推广是什么
  • 做网站至少多少钱重庆seo网络推广平台
  • 温州网站推广公司网站排名怎么搜索靠前
  • 公司网站选择什么空间百度推广登录平台