#define _CRT_SECURE_NO_WARNINGS
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <Windows.h>
#include <memory.h>
#define path "E:\\杂乱test\\内存大数据模型\\1E~001.txt"
char **g_pp;
int imax = 8435714;//标识有多少行
int jmax = 20027;//标识最宽
int getJmax()
{
int width = -1;
FILE *pf = fopen(path, "r");//读文件打开路径
if (pf == NULL)
{
printf("文件打开失败");
}
else
{
while (!feof(pf))
{
char readStr[30000] = { 0 };
fgets(readStr, 30000, pf);//读取一行
readStr[29999] = '\0';//最后为字符串结束
int strLength = strlen(readStr);
if (strLength > width)
{
width = strLength;
}
}
fclose(pf);//关闭
}
return width;
}
int getImax()
{
int hang = -1;
FILE *pf = fopen(path, "r");//读文件打开路径
if (pf == NULL)
{
hang = -1;
printf("文件打开失败");
}
else
{
while (!feof(pf))
{
char readStr[1024] = { 0 };
fgets(readStr, 1024, pf);//读取一行
hang++;
}
fclose(pf);//关闭
}
return hang;
}
void loadFromFile()
{
g_pp = (char **)malloc(sizeof(char*)*imax);//分配指针数组 多少行
memset(g_pp, '\0', sizeof(char*)*imax);
FILE *pf = fopen(path, "r");
if(pf == NULL)
{
printf("文件打开失败");
return;
}
else
{
for (int i = 0; i < imax;i++)
{
char str[1024] = { 0 };
fgets(str, 1024, pf);//按行读取
str[1023] = '\0';
int strLength = strlen(str);
if (strLength < 50)
{
g_pp[i] = malloc(sizeof(char)*(strLength + 1));
strcpy(g_pp[i], str);//拷贝到分配的内存
}
}
fclose(pf);
printf("载入完成\n");
}
}
void search(char *str)
{
if (g_pp != NULL)
{
for (int i = 0; i < imax; i++)
{
if (g_pp[i] != NULL)
{
char *p = strstr(g_pp[i], str);//找到返回地址,否则返回NULL
if (p != NULL)
{
puts(g_pp[i]);//打印
}
}
}
}
}
void main()
{
loadFromFile();
while (1)
{
char str[100] = { 0 };
scanf("%s", str);
search(str);//检索
}
system("pause");
}