数据结构源码笔记(C语言):索引文件建立和查找

//实现索引文件建立和查找算法

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<stdlib.h>


#define MaxRec 100//最多的记录个数


typedef struct Index //定义索引文件结构
{
	char num[8];//学号
    long offset;//主文件中的记录号
}Index;


typedef struct sdata//定义主文件结构
{
	char num[8];
	char name[10];
	int sex;
	int age;
	char addr[30];
	char dep[20];
	char spec[20];
}Student;


void DelAll()//清除主文件和索引文件的全部记录
{
	FILE *mfile,*idxfile;
	if((mfile=fopen("main.dat","wb"))==NULL)
	{
		printf(">>不能打开主文件\n");
		return;
	}

	if((idxfile=fopen("idx.dat","wb"))==NULL)
	{
		printf(">>不能打开索引文件\n");
		return;
	}
	fclose(mfile);
	fclose(idxfile);
}

void InsertSort(Index r[],int n)//对r[0......n-1]按递增有序进行直接插入排序
{
	int i,j;
	Index temp;
	for(i=1;i<n;i++)
	{
		temp=r[i];
		j=i-1;
		while(j>=0 && strcmp(temp.num,r[j].num)<0)
		{
			r[j+1]=r[j];//将关键字大于r[i].key的记录后移
			j--;
		}
		r[j+1]=temp;//在j+1处插入r[i]
	}
}

void CreatIdxFile()//建立索引文件
{
	FILE *mfile ,*idxfile;
	Index idx[MaxRec];
	Student st;
	int n=0,i;
	if((mfile=fopen("main.dat","rb"))==NULL)
	{
		printf(">>不能打开主文件\n");
		return;
	}
	if((idxfile=fopen("index.dat","wb"))==NULL)
	{
		printf(">>不能建立索引文件\n");
		return;
	}
	i=0;
	while((fread(&st,sizeof(Student),1,mfile))!=NULL)
	{
		strcpy(idx[i].num,st.num);
		idx[i].offset=++n;
		i++;
	}
	InsertSort(idx,n);//对idx数组按no域值排序
	rewind(idxfile);
	for(i=0;i<n;i++)
		fwrite(&idx[i],sizeof(Index),1,idxfile);
	fclose(mfile);
	fclose(idxfile);
}


void InputMainFile()//添加一个主文件记录
{
	FILE *mfile;
	Student st;
	mfile=fopen("main.dat","ab+");
	if(mfile==NULL)
	{
		printf(">>不能建立主文件\n");
		return;
	}
	printf(">>学号,姓名,性别,年龄,地址,系别,专业:");
	scanf("%s%s%d%d%s%s%s",st.num,st.name,&st.sex,&st.age,st.addr,st.dep,st.spec);
	if(fwrite(&st,sizeof(Student),1,mfile)!=1)
	{
		printf(">> 写主文件错误\n");
		return;
	}
	fclose(mfile);
}

void OutputMainFile()//输出主文件全部记录
{
	FILE *mfile;
	Student st;
	int i=0;
	mfile=fopen("main.dat","rb");
	if(mfile==NULL)
	{
		printf(">>不能读主文件\n");
		return;
	}
	while((fread(&st,sizeof(Student),1,mfile))!=NULL)
	{
		printf(">> 记录号%d:",++i);
		printf("%s%s%d%d%s%s%s\n",st.num,st.name,st.sex,st.age,st.addr,st.dep,st.spec);
	}
	fclose(mfile);
}

void OutputIdxFile()//输出索引文件全部记录
{
	FILE *idxfile;
	Index irec;
	int i=0;
	idxfile=fopen("index.dat","rb");
	if(idxfile==NULL)
	{
		printf(">>不能读索引文件\n");
		return;
	}
	while((fread(&irec,sizeof(Index),1,idxfile))!=NULL)
		printf(">>(学号:记录号)%s:  %d\n",irec.num,irec.offset);
	fclose(idxfile);
}

void ReadIndexFile(Index idx[MaxRec],int &len)//读索引文件数据存入idx数组中
{
	FILE *idxfile;
	int j;
	if((idxfile=fopen("index.dat","rb"))==NULL)
	{
		printf(">>索引文件不能打开\n");
		return;
	}
	fseek(idxfile,0,2);
	j=ftell(idxfile);
	len=j/sizeof(Index);
	fread(idx,sizeof(Index),len,idxfile);
	fclose(idxfile);
}

int SearchNum(Index idx[],int len,char no[])//在索引文件中查找no对应的记录号
{
	int mid,low ,high,comp;
	low=0;high=len-1;
	while(low<=high)
	{
		mid=(low+high)/2;
		comp=strcmp(idx[mid].num,no);
		if(comp>0)
			high=mid-1;
		else if(comp<0)
			low=mid+1;
		else 
			return idx[mid].offset;
	}
	return -1;
}

void FindStudent()
{
	FILE *mfile;
	char no[8];
	Index idx[MaxRec];
	Student st;
	int i,len;
	mfile=fopen("main.dat","rb+");
	if(mfile==NULL)
	{
		printf(">>主文件中没有任何记录\n");
		return;
	}
	ReadIndexFile(idx,len);//读索引数组idx
	printf("输入学号:");
	scanf("%s",no);
	i=SearchNum(idx,len,no);//在idx中查找
	if(i==-1)
		printf(">>学号 %s 不存在\n",no);
	else
	{
		fseek(mfile,(i-1)*sizeof(Student),SEEK_SET);//由序号直接跳到主文件的这个记录
		fread(&st,sizeof(Student),1,mfile);
		printf(">>%s%s%d%d%s%s%s\n",st.num,st.name,st.sex,st.age,st.age,st.dep,st.spec);
	}
	fclose(mfile);
}

int main()
{
	int sel;
	do
	{
		printf("1:输入 2:输入主文件 3:输出索引文件 4:按学号查找 9:全清  0:退出:");
        scanf("%d",&sel);
		switch(sel)
		{
			case 9:
				DelAll();
			case 1:
				InputMainFile();
				CreatIdxFile();
				break;
			case 2:
				OutputMainFile();
				break;
			case 3:
				OutputIdxFile();
				break;
			case 4:
				FindStudent();
				break;
		}
	}while(sel!=0);
	return 0;
}

数据结构源码笔记(C语言描述)汇总:

数据结构源码笔记(C语言):英文单词按字典序排序的基数排序

数据结构源码笔记(C语言):直接插入排序

数据结构源码笔记(C语言):直接选择排序

数据结构源码笔记(C语言):置换-选择算法

数据结构源码笔记(C语言):Huffman树字符编码

数据结构源码笔记(C语言):Josephus问题之顺序表

数据结构源码笔记(C语言):Josephus问题之循环链接表

数据结构源码笔记(C语言):多项式合并

数据结构源码笔记(C语言):二叉树之叶子结点旋转销毁

数据结构源码笔记(C语言):哈夫曼树

数据结构源码笔记(C语言):集合的位向量表示

数据结构源码笔记(C语言):链接队列

数据结构源码笔记(C语言):链接栈

数据结构源码笔记(C语言):线性表的单链表示

数据结构源码笔记(C语言):线性表的顺序表示

数据结构源码笔记(C语言):栈的基本操作

数据结构源码笔记(C语言):中缀表达式

数据结构源码笔记(C语言):希尔插入排序

数据结构源码笔记(C语言):索引文件建立和查找

数据结构源码笔记(C语言):冒泡排序

数据结构源码笔记(C语言):快速排序

数据结构源码笔记(C语言):可变长度字符串的快速排序

数据结构源码笔记(C语言):基数排序

数据结构源码笔记(C语言):二路归并排序

数据结构源码笔记(C语言):堆排序

数据结构源码笔记(C语言):二叉树搜索树Kruskal

数据结构源码笔记(C语言):二叉搜索树Prim

数据结构源码笔记(C语言):最短路径弗洛伊德算法

数据结构源码笔记(C语言):深度、广度优先生成树

数据结构源码笔记(C语言):邻接矩阵转化邻接表

数据结构源码笔记(C语言):统计字符串中出现的字符及其次数

数据结构源码笔记(C语言):顺序查找

数据结构源码笔记(C语言):哈希表的相关运算算法

数据结构源码笔记(C语言):分块法查找

数据结构源码笔记(C语言):二分查找

数据结构源码笔记(C语言):二叉树遍历

数据结构源码笔记(C语言):二叉平衡树的相关操作算法

数据结构源码笔记(C语言):二叉排序树的基本操作算法

数据结构源码笔记(C语言):B树的相关运算算法

  • 10
    点赞
  • 58
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
在C语言中,可以使用一些库来执行HTTP的GET和POST请求,并下载文件。在这里,我们将使用libcurl库来完成这些操作。 首先,我们需要在代码中引入libcurl库的头文件。 ```c #include <stdio.h> #include <curl/curl.h> ``` 然后,我们可以定义一个回调函数,用于处理下载的数据。 ```c size_t write_callback(void* ptr, size_t size, size_t nmemb, FILE* stream) { return fwrite(ptr, size, nmemb, stream); } ``` 接下来,我们可以编写一个函数来执行HTTP的GET请求,并将返回的数据保存到文件中。 ```c void http_get(const char* url, const char* file_path) { FILE* file = fopen(file_path, "wb"); if (file == NULL) { printf("无法打开文件!\n"); return; } CURL* curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { printf("请求失败:%s\n", curl_easy_strerror(res)); } curl_easy_cleanup(curl); } fclose(file); } ``` 最后,我们可以编写一个函数来执行HTTP的POST请求,并将返回的数据保存到文件中。 ```c void http_post(const char* url, const char* post_data, const char* file_path) { FILE* file = fopen(file_path, "wb"); if (file == NULL) { printf("无法打开文件!\n"); return; } CURL* curl = curl_easy_init(); if (curl) { curl_easy_setopt(curl, CURLOPT_URL, url); curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION, 1L); curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, write_callback); curl_easy_setopt(curl, CURLOPT_WRITEDATA, file); curl_easy_setopt(curl, CURLOPT_POSTFIELDS, post_data); CURLcode res = curl_easy_perform(curl); if (res != CURLE_OK) { printf("请求失败:%s\n", curl_easy_strerror(res)); } curl_easy_cleanup(curl); } fclose(file); } ``` 通过使用以上两个函数,我们可以下载一个文件到指定的路径。例如,假设我们要下载一个名为"test.txt"的文件,可以使用以下代码: ```c http_get("http://example.com/test.txt", "test.txt"); ``` 或者,如果我们需要通过POST请求下载文件,可以使用以下代码: ```c const char* post_data = "param1=value1&param2=value2"; http_post("http://example.com/download", post_data, "test.txt"); ``` 以上就是使用C语言执行HTTP的GET和POST请求,并下载文件的示例代码。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

半个冯博士

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值