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

📄 get_last.c

📁 一个c语言开发的小型的dbms系统
💻 C
字号:
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#include <malloc.h>
#include "struct.h"

void get_last(struct tab_dic_type table,char *data)
{FILE *tab_fp,*data_fp;
 int current_block,i;
 struct data_head head_data;
 //char  data[1024];
 char *data_temp;
 int  *int_data,*int_data_temp;
 int data_size;
 struct tab_dic_type free_block;
 int position;
 int last;
 
 data_size=table.row_len;
 //data=(char *)malloc(data_size);
 data_temp=(char *)malloc(data_size);
 //初始化数据块
 for(i=0;i<data_size;i++)
	 data_temp[i]='a';

 if((data_fp=fopen("DATABASE","rb+"))==NULL)
   {
	 
	 printf("cann't open file DATABASE\n");
    exit(0);
   }/*打开数据文件*/
 rewind(data_fp);

 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);
	}

 if (head_data.next_free_addr-data_size>current_block*1024+24)
 {//修改数据文件的块头信息
	head_data.next_free_addr=head_data.next_free_addr-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");

	//改动空闲存储链
	int_data_temp=data_temp;
	*int_data_temp=head_data.next_free_addr+data_size;
	position=head_data.next_free_addr;
	//数据文件定位本表最后一条纪录
	fseek(data_fp,head_data.next_free_addr,0);
	//读取本表最后一条纪录
	fread(data,data_size,1,data_fp);
	
	//改动空闲存储链
	fseek(data_fp,position,0);
	if (fwrite(data_temp,data_size,1,data_fp)!=1)
			printf("_data file write error!\n");
	}
 else
 {//修改空闲块头信息
	head_data.next_block=free_block.first_free_block;
	last=head_data.last_block;
	head_data.last_block=-2;
	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");
	
  //修改空闲块信息
  free_block.first_free_block=current_block;
  fseek(tab_fp,0,0);
  if (fwrite(&free_block,sizeof(struct tab_dic_type),1,tab_fp)!=1)
            printf("_tab_dic file(free_block) write error!\n");

  	//数据文件定位本表最后一条纪录
	fseek(data_fp,head_data.next_free_addr-data_size,0);
	//读取本表最后一条纪录
	fread(data,data_size,1,data_fp);
  
  //修改数据文件的块头信息
  current_block=last;
  fseek(data_fp,current_block*1024,0);
  fread(&head_data,sizeof(struct data_head),1,data_fp);
  	head_data.next_free_addr=head_data.next_free_addr;
	head_data.next_block=-1;
  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");

	/*改动空闲存储链
	int_data_temp=data_temp;
	*int_data_temp=head_data.next_free_addr+data_size;
	position=head_data.next_free_addr;
	数据文件定位本表最后一条纪录
	fseek(data_fp,head_data.next_free_addr,0);
	读取本表最后一条纪录
	fread(&data,data_size,1,data_fp);
	fseek(data_fp,position,0);
	if (fwrite(data_temp,data_size,1,data_fp)!=1)
			printf("_data file write error!\n");*/

 }
	
	//修改表字典信息
	table.row_num=table.row_num-1;
	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);

}

⌨️ 快捷键说明

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