📄 insert.c
字号:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "struct.h"
#include <time.h>
void insert(struct tab_dic_type table,struct col_dic_type *table_fields,int ft_num,struct col_dic_type *fields,int f_num,char values[10][40])
{char *data,*data1,*data_addr;
int *int_data,*int_data1,*int_data_addr;
//char test_data[20];
int offset[10];
int offset1;//数据块偏移地址
int i,j,data_size;
int current_block,last_block;
struct data_head head_data,head_data_temp;
struct tab_dic_type free_block;
FILE *data_fp,*tab_fp;
//test
// get_last(table);
offset[0]=0;
data_size=0;
data_size=table.row_len;
//开辟数据块空间
data=(char *)malloc(data_size);
data_addr=(char *)malloc(data_size);
if((data_fp=fopen("DATABASE","rb+"))==NULL)
{printf("cann't open file DATABASE\n");
exit(0);
}/*打开数据文件*/
if((tab_fp=fopen("TABLE","rb+"))==NULL)
{printf("cann't open file TABLE\n");
exit(0);
}/*打开表字典文件*/
fread(&free_block,sizeof(struct tab_dic_type),1,tab_fp);
//选择表的空闲数据块
current_block=table.first_free_block;
fseek(data_fp,table.first_free_block*1024,0);
fread(&head_data,sizeof(struct data_head),1,data_fp);
while (head_data.next_block!=-1)
{current_block=head_data.next_block;
fseek(data_fp,current_block*1024,0);
fread(&head_data,sizeof(struct data_head),1,data_fp);
}
fseek(data_fp,head_data.next_free_addr,0);
fseek(tab_fp,table.tab_id*sizeof(struct tab_dic_type),0);
for (i=1;i<f_num;i++)
offset[i]=offset[i-1]+table_fields[i-1].col_length+1;
//读取下一空闲块地址并恢复文件指针到原位
fread(data_addr,data_size,1,data_fp);
int_data_addr=data_addr;
fseek(data_fp,head_data.next_free_addr,0);
for(i=0;i<data_size;i++)
data[i]='\0';
for(i=0;i<f_num;i++)
{j=fields[i].col_id-100*fields[i].tab_id;
if (fields[i].col_type==0)
{ int_data=&data[offset[j]];
*int_data=atoi(&values[i]);
}
else
strcpy(&data[offset[j]],values[i]);
}
//strcpy(&test_data,&data[21]);
table.row_num=table.row_num+1;
//修改数据块头信息
//if (*int_data_addr<=(table.first_free_block+1)*1024)
if (*int_data_addr<(current_block+1)*1024)
{head_data.next_free_addr=*int_data_addr;
fflush(data_fp);
if (fwrite(data,data_size,1,data_fp)!=1)
printf("_data file write error!\n");
fseek(data_fp,current_block*1024,0);
if (fwrite(&head_data,sizeof(struct data_head),1,data_fp)!=1)
printf("_data file(data_head) write error!\n");
}
else
{head_data.next_block=free_block.first_free_block;//块头信息更新
fseek(data_fp,current_block*1024,0);
if (fwrite(&head_data,sizeof(struct data_head),1,data_fp)!=1)
printf("_data file(data_head) write error!\n");
//表空间扩展处理
last_block=current_block;
current_block=free_block.first_free_block;
//读取空闲块块头信息
fseek(data_fp,free_block.first_free_block*1024,0);
fflush(data_fp);
fread(&head_data_temp,sizeof(struct data_head),1,data_fp);
if (head_data_temp.last_block==-2)
free_block.first_free_block=head_data_temp.next_block;
else
free_block.first_free_block=free_block.first_free_block+1;//第一空闲块更新
fseek(tab_fp,0,0);
fflush(data_fp);
if (fwrite(&free_block,sizeof(struct tab_dic_type),1,tab_fp)!=1)
printf("_tab_dic file(free_block) write error!\n");
//填写数据头信息
head_data.next_block=-1;
head_data.last_block=last_block;
head_data.next_free_addr=current_block*1024+24+data_size;
fseek(data_fp,current_block*1024,0);
if (fwrite(&head_data,sizeof(struct data_head),1,data_fp)!=1)
printf("_data file(data_head) write error!\n");
fseek(data_fp,current_block*1024+24,0);
offset1=ftell(data_fp);
while (offset1<=(free_block.first_free_block*1024-1))
{data1=(char *)malloc(table.row_len);
int_data1=data1;
*int_data1=offset1+table.row_len;
offset1=offset1+table.row_len;
if (fwrite(data1,table.row_len,1,data_fp)!=1)
printf("_data file write error!\n");
free(data1);
}
fseek(data_fp,current_block*1024+24,0);
if (fwrite(data,data_size,1,data_fp)!=1)
printf("_data file write error!\n");
}
fseek(tab_fp,table.tab_id*sizeof(struct tab_dic_type),0);
if (fwrite(&table,sizeof(struct tab_dic_type),1,tab_fp)!=1)
printf("_tab_dic file write error!\n");
fclose(tab_fp);
//fclose(col_fp);
fclose(data_fp);
free(data_addr);
free(data);
//free(data1);
// if( fclose( data_fp ) )
// printf( "The file 'data' was not closed\n" );
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -