从文件文件中把密码提取出来,并进行排序。
#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");
}