密码提取-排序

4 篇文章 0 订阅
3 篇文章 0 订阅

从文件文件中把密码提取出来,并进行排序。


#define  _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
#include<string.h>

char path[50] = "e:\\ok.txt";
char indexpath[50] = "e:\\index.txt";
char sordpath[50] = "e:\\sort.txt";

struct info
{
	long long qq;
	int index;		//保存索引
};
typedef struct info Index;

void getQQ(char *str, char *temp)
{
	strcpy(temp, str);
	char *p = temp;
	while (*p)
	{
		if (*p == '-')
		{
			*p = '\0';
			break;
		}
		p++;
	}

}


void init(char *path, char *indexpath)
{
	FILE *pfr = fopen(path, "r");
	FILE *pfw = fopen(indexpath, "wb");
	if (pfr == NULL || pfw == NULL)
	{
		return;
	}
	else
	{
		while (!feof(pfr))
		{
			/*
				下面二个变量的长度最好设置为一至,因为后面要对这二个字符串进行赋值,
				而str 的长度大于 qq的长度,导致 赋值溢出。这也是偶然一次写错文件名
				后出现的一个错误。因为前对这个文件的每行的字符数据控制在 50 以内,而
				原文件的一行数据最多达到 255 。
			*/
			/*char str[256] = {0};	
			char qq[50] = { 0 };*/
			char str[50] = { 0 };
			char qq[50] = { 0 };
			int index = ftell(pfr);		//获取指针位置
			fgets(str, 50, pfr);

			
			getQQ(str, qq); //截取QQ号
			long long tempqq = atoll(qq);
		
			if (tempqq == 0)	//转换失败后继续
			{
				continue;
			}
			Index infoq = { 0 };
			infoq.index = index;
			infoq.qq = tempqq;	

			fwrite(&infoq, sizeof(infoq), 1, pfw);	//写入索引
		}
	}

	fclose(pfr);
	fclose(pfw);
	printf("初始化完成!\n");
}

int getmaxline(char *path)
{
	int max = 0;
	FILE *pfr = fopen(path, "r");

	while (!feof(pfr))
	{
		char str[256] = { 0 };
		fgets(str, 256, pfr);
		int length = strlen(str);
		if (max < length)
		{
			max = length;
		}
	}
	fclose(pfr);
	printf("字符串最长长度:%d\n", max);
	return max;
}

void getLine(char *path)
{
	FILE *pfr = fopen(path, "rb");
	int max = 0;
	while (!feof(pfr))
	{
		char str[256] = { 0 };
		fgets(str, 256, pfr);
		max++;
	}
	fclose(pfr);
	printf("总共有:%d 行 \n", max);
	return max;

}


void main1()
{
	
	//init(path, indexpath);
	//printf("初始化开始 !\n");

	getmaxline(path);
	//getLine(path);

	//printf("初始化完成!\n");

	//system("pause");
}

int com(void *p1, void *p2)
{	//按数字排序
	struct info *pinfo1 = p1;
	struct info *pinfo2 = p2;
	if (pinfo1->qq > pinfo2->qq)
	{
		return 1;
	}
	else if (pinfo1->qq < pinfo2->qq)
	{
		return -1;
	}
	else
	{
		return 0;
	}
}

void getsrotqq(char *path)
{
	FILE *pfr = fopen(path, "rb");
	
	while (!feof(pfr))
	{
		struct info pinfo = { 0 };
		fread(&pinfo, sizeof(struct info), 1, pfr);

		printf("%lld-------%d\n", pinfo.qq, pinfo.index);
		//system("pause");
		
	}
	fclose(pfr);
}

void getindexqq(char *path)
{
	FILE *pfr = fopen(path, "rb");

	while (!feof(pfr))
	{
		struct info pinfo = { 0 };
		fread(&pinfo, sizeof(struct info), 1, pfr);

		printf("%lld-------%d\n", pinfo.qq, pinfo.index);
		system("pause");

	}
	fclose(pfr);
}



void main()
{
	//FILE *pfr = fopen(indexpath, "rb");
	//FILE *pfw = fopen(sordpath, "wb");

	//struct info *pinfo = calloc(84331509, sizeof(struct info));
	//if (pinfo == NULL)
	//{
	//	return;
	//}
	//fread(pinfo, sizeof(struct info), 84331509, pfr);		//写到内存
	//qsort(pinfo, 84331509, sizeof(struct info), com);

	//fwrite(pinfo, sizeof(struct info), 84331509, pfw);


	//fclose(pfr);
	//fclose(pfw);

	getsrotqq(sordpath);




	system("pause");
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值