⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 update.c

📁 一个用C编写的简单的关系数据库系统
💻 C
字号:
void Update(DBF * dbf_name, char * commond, FILE ** opened_fp)
{
	RecUnion rec[MAX_FILED_COUNT]; 
	RecUnion * recIn = (RecUnion *)malloc(sizeof(RecUnion));
	RecUnion * tempRec = (RecUnion *)malloc(sizeof(RecUnion));

	char yes = 'n';
	
	int update[MAX_FILED_COUNT] = {0};/*标记各个字段是否修改值*/
	int q = 0;
	int i = 0;
	int xiaoshu = FALSE;
	int tempInt = 0;
	int fpi;
	int pos1 = -1;
	int pos2 = -1;
	int op = -1;
	int isString = FALSE;
	int type1 = -1;
	int type2 = -1;
	int uCount = 0;/*记录总的纪录修改数*/
	int offset1 = 0;
	int offset2 = 0;
	int q1 = -1;/*记录关系运算符第一个运算对象,字段的字段号*/
	int q2 = -1;/*如果关系运算符第一个运算对象为字段则记下其字段号*/
	int limited = FALSE;
	int compare = ERROR;

	unsigned j = 0;

	float div = 1;
	float tempFloat = 0;
	
	commond = strtok(commond," ");
	commond = strtok(NULL," ");

	fpi = dbf_name->dhID;

	if (strcmp(dbf_name->dhName,"\0") == 0)
	{
		puts("	您还没有打开过文件,当前文件为空!!!");
		return;
	}
	if (commond == NULL)
	{
		puts("	格式错误,请输入等号及新值");
		return;
	}
	fseek(opened_fp[fpi],sizeof(DBF),0);
	while ((commond != NULL) && (strcmpi(commond,"WHERE") != 0))
	{/*读需要修改的字段*/
		for (q=0; q<dbf_name->dhFieldCount; q++)
		{
			if (strcmpi(dbf_name->dbf_struct[q].dhFieldName,commond) == 0)
			{
				if (update[q] == 1)
				{
					printf("	错误,该属性%s已输入过\n",commond);
					return;
				}
				break;
			}
		}
		if ((q >= dbf_name->dhFieldCount) && !update[q-1])
		{
			printf("	格式错误,不存在 %s 属性\n", commond);
			return;
		}
		commond = strtok(NULL, " ");
		if (strcmp(commond,"=") != 0)
		{
			printf("	输入的字符串%s该为等号\n",commond);
			return;
		}
		commond = strtok(NULL, " ");
		if (commond == NULL)
		{
			puts("	格式错误,请输入新值");
			return;
		}
		switch (dbf_name->dbf_struct[q].dhFieldType) {
		case Int:/*读入新值*/
			for (j=0; j<strlen(commond); j++)
			{
				if (!isdigit(commond[j]))
				{
					printf("	输入错误,%s 改为int型\n",commond);
					return;
				}
			}
			rec[q].intType = atoi(commond);
			break;
		case Float:
				for (j=0; j<strlen(commond); j++)
				{
					if (isdigit(commond[j]))
					{
						tempFloat = tempFloat * 10 + (int)commond[j] - 48;
						if (xiaoshu == TRUE)
						{
							div *= 10;
						}
					}
					else if (commond[j] == '.')
					{
						if (xiaoshu == TRUE)
						{
							printf("	输入错误,%s 改为float型\n",commond);
							return;
						}
						xiaoshu = TRUE;
					}
					else
					{
						printf("	输入错误,%s 改为float型\n",commond);
						return;
					}
				}
				rec[q].floatType = tempFloat / div;
				break;
		case String:
			if (strlen(commond) > (unsigned)dbf_name->dbf_struct[q].dhFieldLen)
			{
				printf("	输入字符串超出了定义的最大长度 %d\n",dbf_name->dbf_struct[q].dhFieldLen);
				return;
			}
			rec[q].string = (char *)malloc(dbf_name->dbf_struct[q].dhFieldLen * sizeof(char));
			strcpy(rec[q].string, commond);
			break;
		}
		update[q] = 1;/*标记该修改的字段*/
		commond = strtok(NULL, " ");
	}
	/*读选择条件*/
	if ((commond != NULL) && (strcmpi(commond,"WHERE") == 0))
	{
		commond = strtok(NULL, " ");
		if (commond == NULL)
		{
			puts("	请输入选择条件");
			return;
		}
		for (q=0; q<dbf_name->dhFieldCount; q++)
		{
			if (strcmpi(dbf_name->dbf_struct[q].dhFieldName,commond) == 0)
			{
				pos1 = dbf_name->dbf_struct[q].dhFieldPos;
				type1 = dbf_name->dbf_struct[q].dhFieldType;
				offset1 = dbf_name->dbf_struct[q].dhFieldLen;
				q1 = q;
				break;
			}
		}
		if (pos1 == -1)
		{
			printf("	该关系不存在字段 %s\n",commond);
			return;
		}
		commond = strtok(NULL," ");
		/*读关系运算符*/
		if (commond == NULL)
		{
			puts("	命令格式错误,请输入关系运算符");
			return;
		}
		if (strcmpi(commond,"<") == 0)
			op = LT;
		else if (strcmpi(commond,"<=") == 0)
			op = LE;
		else if (strcmpi(commond,"=") == 0)
			op = EQ;
		else if (strcmpi(commond,">") == 0)
			op = GT;
		else if (strcmpi(commond,">=") == 0)
			op = GE;
		else if (strcmpi(commond,"<>") == 0)
			op = NE;
		else
		{
			puts("	输入的关系运算符错误!!!");
			return; 
		}
		/*读关系运算符的第二个比较对象*/
		commond = strtok(NULL," ");
		if (commond == NULL)
		{
			puts("	命令格式错误,请输入关系运算符的下个运算对象");
			return;
		}
		/*判断第二个运算对象类型,并记录其值*/
		if ((commond[0] == '0') && (strlen(commond) > 1))
		{	
			isString = TRUE;
			type2 = String;
		}
		else
		{
			for (j=0; j<strlen(commond); j++)
			{
				if (isdigit(commond[j]))
				{
					tempFloat = tempFloat * 10 + (int)commond[j] - 48;
					tempInt = tempInt * 10 + (int)commond[j] - 48;
					if (xiaoshu == TRUE)
						div *= 10;
				}
				else if (commond[j] == '.')
				{
					if (xiaoshu == TRUE)
					{
						isString = TRUE;
						break;
					}
					xiaoshu = TRUE;
				}
				else
				{
					isString = TRUE;
					break;
				}
			}
		}
		if (isString)
		{
			tempRec->string = (char *)malloc(strlen(commond) * sizeof(char));
			strcpy(tempRec->string,commond);
			/*如果关系运算符的第二个对象为字段名,用pos2记录其偏移位置*/
			for (q=0; q<dbf_name->dhFieldCount; q++)
			{
				if (strcmpi(dbf_name->dbf_struct[q].dhFieldName,commond) == 0)
				{
					pos2 = dbf_name->dbf_struct[q].dhFieldPos;
					type2 = dbf_name->dbf_struct[q].dhFieldType;
					offset2 = dbf_name->dbf_struct[q].dhFieldLen;
					q2 = q;
					break;
				}
			}
			type2 = String;
		}
		else if (xiaoshu)
		{
			tempRec->floatType = tempFloat / div;
			type2 = Float;
		}
		else
		{
			tempRec->intType = tempInt;
			type2 = Int;
		}
		limited = TRUE;/*标记输入了条件*/
	}
	/*修改记录*/
	for (i=0; i<dbf_name->dhRecCount; i++)
	{
		if (limited)
		{	/*比较看是否满足显示条件*/
			fseek(opened_fp[fpi],pos1,1);
			switch (type1) {
			case Int:
				fread(recIn,sizeof(int),1,opened_fp[fpi]);
				break;
			case Float:
				fread(recIn,sizeof(float),1,opened_fp[fpi]);
				break;
			case String:
				recIn->string = (char *)malloc(dbf_name->dbf_struct[q1].dhFieldLen * sizeof(char));
				fread(recIn->string,dbf_name->dbf_struct[q1].dhFieldLen,1,opened_fp[fpi]);
				recIn->string[dbf_name->dbf_struct[q1].dhFieldLen] = '\0';
				break;
			default:
				break;
			}
			fseek(opened_fp[fpi],-offset1-pos1,1);
			/*如果第二个运算对象是某个字段,则取出该字段的数据到tempRec*/
			if (pos2 != -1)
			{
				fseek(opened_fp[fpi],pos2,1);
				switch (type2) {
				case Int:
					fread(tempRec,sizeof(int),1,opened_fp[fpi]);
					break;
				case Float:
					fread(tempRec,sizeof(float),1,opened_fp[fpi]);
					break;
				case String:
					tempRec->string = (char *)malloc(dbf_name->dbf_struct[q2].dhFieldLen * sizeof(char));
					fread(tempRec->string,dbf_name->dbf_struct[q2].dhFieldLen,1,opened_fp[fpi]);
					tempRec->string[dbf_name->dbf_struct[q2].dhFieldLen] = '\0';
					break;
				default:
					break;
				}
				fseek(opened_fp[fpi],-offset2-pos2,1);
			}
			compare = Compare(recIn,tempRec,type1,type2);
			if (compare == ERROR)
				return;
			switch (op) {/*不满足条件则文件指针指到下一条记录*/
			case LT:
				if (compare != LT)
				{
					fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
					continue;
				}
				break;
			case LE:
				if (compare == GT)
				{
					fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
					continue;
				}
				break;
			case EQ:
				if (compare != EQ)
				{
					fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
					continue;
				}
				break;
			case GT:
				if (compare != GT)
				{
					fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
					continue;
				}
				break;
			case GE:
				if (compare == LT)
				{
					fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
					continue;
				}
				break;
			case NE:
				if (compare == EQ)
				{
					fseek(opened_fp[fpi],dbf_name->dhRecLen,1);
					continue;
				}
				break;
			default:
				puts("ERROR");
					return ;
				break;
			}/*end of switch*/
		}/*end of if (limited)*/
		/*满足条件则进行修改*/
		for (q=0; q<dbf_name->dhFieldCount; q++)
		{
			if (update[q])/*判断该属性是否要置新值*/
			{
				switch (dbf_name->dbf_struct[q].dhFieldType) {
				case Int:
					fwrite(&rec[q].intType,sizeof(int),1,opened_fp[fpi]);
					break;
				case Float:
					fwrite(&rec[q].floatType,sizeof(float),1,opened_fp[fpi]);
					break;
				case String:
					fwrite(rec[q].string,dbf_name->dbf_struct[q].dhFieldLen,1,opened_fp[fpi]);
					break;
				}
			}
			else
			{
				fseek(opened_fp[fpi],dbf_name->dbf_struct[q].dhFieldLen,1);
			}
		}/*end of for q=0*/
		uCount++;
		dbf_name->dhRecNO = i;
	}/*end of for i=0*/
	printf("	update %d records\n", uCount);
	return;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -