文件操作实例

开发数据查询

define _CRT_SECURE_NO_WARNINGS

include <stdio.h>

include <stdlib.h>

include <string.h>

include<memory.h>

include <Windows.h>

define path "kaifang.txt"

char ** g_pp;//全局的二级指针
int imax =20151574;//标示有多少行

int getimax()
{
int hang = -1;
FILE *pf = fopen(path, "r");//读文件打开路径
if (pf == NULL)
{
printf("文件打开失败");
return -1;
}
else
{
hang = 0;
while (!feof(pf))//到了文件末尾返回1,没有返回0
{
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 -1;
}
else
{
for (int i = 0; i < imax; i++)
{
char str[1024] = { 0 };
fgets(str, 1024, pf);//按行读取
str[1024 - 1] = '\0';
int strlength = strlen(str);

g_pp[i] = malloc(sizeof(char)*(strlength + 1));//处理/0

if (g_pp[i]!=NULL)
{
strcpy(g_pp[i], str);//拷贝到分配的内存
}

}

fclose(pf);//关闭

}
}

void search(char *str)
{
char strpath[100] = {0};
sprintf(strpath, "I:\%s.txt", str);
FILE *pf = fopen(strpath, "w");//写的模式打开

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]);//打印
fputs(g_pp[i], pf);//输出到文件
}
}
}
}

fclose(pf);
system(strpath);//打开文件

}

void main()
{
//printf("%d", getimax());
//printf("%d", getjmax());
loadfromfile();
printf("检测完成\n");

while (1)
{
char str[100] = { 0 };
scanf("%s", str);//初始化str数组

search(str);//检索
}
system("pause");
}

QQ密码查询

define _CRT_SECURE_NO_WARNINGS

include <stdio.h>

include <stdlib.h>

include <string.h>

include<memory.h>

include <Windows.h>

define path "kaifang.txt"

char ** g_pp;//全局的二级指针
int imax =20151574;//标示有多少行

int getimax()
{
int hang = -1;
FILE *pf = fopen(path, "r");//读文件打开路径
if (pf == NULL)
{
printf("文件打开失败");
return -1;
}
else
{
hang = 0;
while (!feof(pf))//到了文件末尾返回1,没有返回0
{
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 -1;
}
else
{
for (int i = 0; i < imax; i++)
{
char str[1024] = { 0 };
fgets(str, 1024, pf);//按行读取
str[1024 - 1] = '\0';
int strlength = strlen(str);

g_pp[i] = malloc(sizeof(char)*(strlength + 1));//处理/0

if (g_pp[i]!=NULL)
{
strcpy(g_pp[i], str);//拷贝到分配的内存
}

}

fclose(pf);//关闭

}
}

void search(char *str)
{
char strpath[100] = {0};
sprintf(strpath, "I:\%s.txt", str);
FILE *pf = fopen(strpath, "w");//写的模式打开

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]);//打印
fputs(g_pp[i], pf);//输出到文件
}
}
}
}

fclose(pf);
system(strpath);//打开文件

}

void main()
{
//printf("%d", getimax());
//printf("%d", getjmax());
loadfromfile();
printf("检测完成\n");

while (1)
{
char str[100] = { 0 };
scanf("%s", str);//初始化str数组

search(str);//检索
}
system("pause");
}

posted @ 2016-11-18 21:00  呉语伦比  阅读(143)  评论(0)    收藏  举报