📄 insert.c
字号:
int Insert(DBF * dbf_name, char * commond, FILE ** opened_fp)
{
RecUnion rec[MAX_FILED_COUNT];
RecUnion * recIn = (RecUnion *)malloc(sizeof(RecUnion));
int i = 0;
int q = 0;/*循环变量*/
int exist = TRUE;
int xiaoshu = FALSE;
unsigned j = 0;
float div = 1;/*记录小数位数*10*/
float tempFloat = 0;
int fpi = -1;/*标记当前表在打开表数组中的号码*/
commond = strtok(commond," ");
commond = strtok(NULL," ");
if (strcmp(dbf_name->dhName,"\0") == 0)
{
puts(" 您还没有打开过文件,当前文件为空!!!");
return FAILED;
}
i = 0;
while (i < dbf_name->dhFieldCount)/*读取数据*/
{
if (commond == NULL)
{
printf(" 格式错误,请输入 %s 属性 \n", dbf_name->dbf_struct[i].dhFieldName);
return ERROR;
}
switch (dbf_name->dbf_struct[i].dhFieldType) {
case Int:
for (j=0; j<strlen(commond); j++)
{
if (!isdigit(commond[j]))
{
printf(" 输入错误,%s 改为int型\n",commond);
return ERROR;
}
}
rec[i].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 ERROR;
}
xiaoshu = TRUE;
}
else
{
printf(" 输入错误,%s 改为float型\n",commond);
return ERROR;
}
}
rec[i].floatType = tempFloat / div;
break;
case String:
if (strlen(commond) > (unsigned)dbf_name->dbf_struct[i].dhFieldLen)
{
printf(" 输入字符串超出了定义的最大长度 %d\n",dbf_name->dbf_struct[i].dhFieldLen);
return ERROR;
}
rec[i].string = (char *)malloc(dbf_name->dbf_struct[i].dhFieldLen * sizeof(char));
strcpy(rec[i].string, commond);
break;
}
commond = strtok(NULL," ");
xiaoshu = FALSE;
i++;
}
if (commond != NULL)
{
printf(" 多余输入字符 %s\n",commond);
return ERROR;
}
fpi = dbf_name->dhID;
i = 0;
fseek(opened_fp[fpi],sizeof(DBF),0);
/*检查插入的记录是否已存在*/
while (i < dbf_name->dhRecCount)
{
exist = TRUE;
for (q=0; q<dbf_name->dhFieldCount; q++)
{
switch (dbf_name->dbf_struct[q].dhFieldType) {
case Int:
fread(recIn,sizeof(int),1,opened_fp[fpi]);
if (rec[q].intType != recIn->intType)
exist = FALSE;
break;
case Float:
fread(recIn,sizeof(float),1,opened_fp[fpi]);
if ((rec[q].floatType - recIn->floatType > 0.00001) || (rec[q].floatType - recIn->floatType < -0.00001))
exist = FALSE;
break;
case String:
recIn->string = (char *)malloc(dbf_name->dbf_struct[q].dhFieldLen * sizeof(char));
fread(recIn->string,dbf_name->dbf_struct[q].dhFieldLen,1,opened_fp[fpi]);
if (strcmpi(rec[q].string, recIn->string) != 0)
exist = FALSE;
break;
default:
break;
}
}
if (exist)
break;
i++;
}
i = 0;
if (dbf_name->dhRecCount == 0)/*插入的记录为第一个记录*/
exist = FALSE;
if (exist)
{
printf(" the record inputed already existed!!!\n");
return FAILED;
}
/*插入记录*/
fseek(opened_fp[fpi],0,2);
WriteRec(opened_fp[fpi],dbf_name,rec);
dbf_name->dhModify = 1;
dbf_name->dhRecNO = dbf_name->dhRecCount;
dbf_name->dhRecCount++;
fflush(opened_fp[fpi]);
// chsize(dbf_name->dhHandle, (filelength(dbf_name->dhHandle)+dbf_name->dhRecLen));
/*free(recIn);
recIn = NULL;*/
return SUCCESS;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -