实现大文件里的快速排序

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

struct csdn
{
    char name[22];
    char password[43];
    char email[52];

};
int namemax = -1;
int passmax = -1;
int mailmax = -1;

void init(struct csdn *pdata, char *str)
{
    for (char *p = str; *p != '';p++)
    {
        if (*p=='#')
        {
            *p = '';
        }
    }
    strcpy(pdata->name, str);
    char *pstr1 = str + strlen(str) + 1;
    strcpy(pdata->password, pstr1);
    char *pstr2 = pstr1 + strlen(pstr1) + 1;
    strcpy(pdata->email, pstr2);
    //printf("%s,%s,%s", pdata->name, pdata->password, pdata->email);

}

void getmax(char *str)
{
    for (char *p = str; *p != ''; p++)
    {
        if (*p == '#')
        {
            *p = '';
        }
    }
    int max1 = strlen(str);
    if (max1>namemax)
    {
        namemax = max1;
    }
    char *pstr1 = str + strlen(str) + 1;
    int max2 = strlen(pstr1);
    if (max2>passmax)
    {
        passmax = max2;
    }
    char *pstr2 = pstr1 + strlen(pstr1) + 1;
    int max3 = strlen(pstr2);
    if (max3>mailmax)
    {
        mailmax = max3;
    }

}

void readfiletxt()
{
    FILE *pfr = fopen("Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\csdn.txt", "r");
    FILE *pfw = fopen("Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\csdn.bin", "wb");
    if (pfr==NULL)
    {
        return;
    } 
    else
    {
        while (!feof(pfr))
        {
            char str[256] = { 0 };
            fgets(str, 256, pfr);
            struct csdn csdn1;
            init(&csdn1, str);
            fwrite(&csdn1, sizeof(struct csdn), 1, pfw);//写入
            //getmax(str);
        }

    }



    fclose(pfr);
    fclose(pfw);
}

int getfilesize(char *path)
{
    FILE *pf = fopen(path, "rb");
    if (pf==NULL)
    {
        return -1;
    } 
    else
    {
        fseek(pf, 0, SEEK_END);
        int length = ftell(pf);
        fclose(pf);
        return length;
    }

}
void main()
{
    //struct csdn csdn1;
    //char str[100] = "bamyl # 7618595 # bamyl@etang.com";
    //init(&csdn1, str);
    //readfiletxt();
    //printf("%d,%d,%d", namemax, passmax, mailmax);
    int size=getfilesize("Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\csdn.bin");
    printf("%d", size);
    printf("
%d", size / sizeof(struct csdn));
    FILE *pf = fopen("Z:\I\尹成清华终极版C语言视频源码文档20150131\大数据相关数据\csdn.bin", "rb+");
    while (1)
    {
        printf("请输入你要读取的第N个元素");
        int N;
        scanf("%d", &N);

        struct csdn csdn1 = {0};
        fseek(pf, sizeof(struct csdn)*(N - 1), SEEK_SET);//移动到这个位置
        fread(&csdn1, sizeof(struct csdn), 1, pf);
        printf("
%s,%s,%s", csdn1.name, csdn1.password, csdn1.email);


    }
    fclose(pf);

    system("pause");
}
原文地址:https://www.cnblogs.com/sjxbg/p/5879331.html