文件检索

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

char strpath[256] = "E:\杂乱test\kaifang.txt";
char savepath[256] = { 0 };

void showlist(char str[256])
{
    sprintf(savepath, "E:\杂乱test\%s.txt", str);
    FILE *pf;//文件指针 打开
    pf = fopen(strpath, "r");//读取

    FILE *pfw = fopen(savepath, "w");
    if (pf == NULL)
    {
        printf("文件创建失败");
    }

    if (pf == NULL)
    {
        printf("文件打开失败");
    }
    else
    {
        while (!feof(pf))//没有到文件末尾就继续
        {
            char readstr[1024] = { 0 };
            fgets(readstr, 1024, pf);//读取一行
            char *p = strstr(readstr, str);//字符串查找
            if (p != NULL)
            {
                puts(readstr);//打印
                fputs(readstr, pfw);//写入
            }
        }
    }
    fclose(pf);
}

void main()
{
    char str[256] = { 0 };
    scanf("%s", str);
    printf("你要查询的是%s  
", str);

    time_t start,end;
    time(&start);

    showlist(str);

    time(&end);
    printf("花了%f秒
", difftime(end, start));
    system(savepath);
    system("pause");
}
原文地址:https://www.cnblogs.com/xiaochi/p/5094365.html