单线程实现检索当当网泄露的1GB用户数据
新建項目dangdangusersearch
2.編寫頭文件head.h
#ifndef _HEAD_H_
#define _HEAD_H_
?
#include <stdio.h>??? //注意文件操作相關函數的頭文件是stdio.h
#include<stdlib.h>??? //為了使用system函數
#include <string.h>?? //字符串操作所用到的頭文件
#include<Windows.h>?? //使用休眠函數時用到的頭文件
#include <crtdbg.h>?? //做內存泄露檢測所需的頭文件
?
//開啟內存檢測
#define _CRTDBG_MAP_ALLOC
?
/*菜單查看*/
extern void main_view();
?
/*獲得文件的字節大小*/
extern int getFileSize(const char *path);
/*加載文件,fPath為傳遞進去的文件路徑*/
extern void loadFile(const char *fPath, const int rowSize);
/*通過關鍵字查找*/
extern void findStringByKeyword(const char *keyword, const int rowSize);
/*查看文件中有多少行*/
extern int getFileRow(const char *path);
/*釋放存放每行直至的數組的內存和每行字符串所占的內存*/
extern void freeMemory(char **pp, const int rowSize);
?
#endif
3.編寫main.c
//注意文件操作相關函數的頭文件是stdio.h
#include "head.h"
?
/*菜單查看*/
void main_view()
{
??? system("cls");
??? printf("\n******************當當用戶信息查詢系統*******************\n");
??? printf("\t1.載入數據至內存(-l)\n");
??? printf("\t2.查找字符串(-s)\n");
??? printf("\t3.釋放內存(-f)\n");
??? printf("\n*************************0.退出**************************\n");
}
4.編寫file.c
#define _CRT_SECURE_NO_WARNINGS
#include "head.h"
?
char **pp = NULL; //存儲指針數組的地址
FILE *pf = NULL;? //定義一個文件指針
int flag = 0;???? //這個標識符用于判斷是否加載完成,0:標識沒有加載,1:表示完成加載,2:表示釋放釋放內存
?
/*獲得文件的字節大小*/
int getFileSize(const char *path)
{
??? //按照讀取的模式打開
??? pf = fopen(path, "r");
??? if (pf == NULL)
??? {
??????? //代表獲取文件失敗
??????? return -1;
??? }
??? else
??? {
??????? //fseek():Moves thefile pointer to a specified location.
??????? fseek(pf, 0, SEEK_END);
??????? //ftell( FILE*stream ):文件開頭到當前位置有多少個字節
??????? int num = ftell(pf);
??????? fclose(pf);//關閉文件
??????? return num;
??? }
}
?
/*加載文件,fPath為傳遞進去的文件路徑*/
void loadFile(const char *fPath, const int rowSize)
{
??? printf("加載文件中,請稍后....");
??? //pp里面存儲的是每行字符串的地址
??? pp = (char **)malloc(sizeof(char *)*rowSize);
??? pf = fopen(fPath, "r");
??? if (pf == NULL)
??? {
??????? printf("對不起,加載文件失敗!");
??????? return;
??? }
??? else
??? {
??????? int i;
??????? for (i = 0; i < rowSize; i++)
??????? {
??????????? //讀取字符串的緩沖區
??????????? char str[275] = { 0 };
??????????? //*fgets( char*string, int n, FILE *stream );
??????????? //從文件中逐行讀取字符串
??????????? fgets(str, 275, pf);
??????????? //獲取要分配的字符串長度,最后加一是因為'\0'
??????????? int strlength = strlen(str) + 1;
??????????? //分配內存
??????????? char *px = malloc(sizeof(char)*strlength);
??????????? //拷貝字符串
??????????? strcpy(px, str);
??????????? //設定最后一個字符串為'\0'
??????????? px[strlength - 1] = '\0';
??????????? //存儲字符串的首地址到指針數組
??????????? pp[i] = px;
??????? }
??? }
??? fclose(pf);
??? flag = 1;
??? printf("載入內存OK\n");
}
?
/*通過關鍵字查找*/
void findStringByKeyword(const char *keyword, const int rowSize)
{
??? if (pp == NULL)
??? {
??????? printf("對不起,您還沒有加載文件,請您先加載文件\n");
??????? return;
??? }
??? int i;
??? for (i = 0; i < rowSize; i++)
??? {
??????? //遍歷所有的指針數組的地址,字符串查找
??????? char *ptemp = strstr(pp[i], keyword);
??????? if (ptemp != NULL)
??????? {
??????????? printf("\n%s", pp[i]);//打印字符串
??????? }
??? }
}
?
int getFileRow(const char *path)
{
??? //讀取的模式打開
??? pf = fopen(path, "r");
??? if (pf == NULL)
??? {
??????? //代表獲取失敗
??????? return -1;
??? }
??? else
??? {
??????? int i = 0;
??????? //是否到文件末尾
??????? while (!feof(pf))
??????? {
??????????? char str[275];
??????????? fgets(str, 275, pf);//讀取一行
??????????? i++;
??????? }
??????? fclose(pf);
??????? return i;
??? }
}
?
/*釋放存放每行直至的數組的內存和每行字符串所占的內存*/
void freeMemory(char **pp, const int rowSize)
{
??? printf("正在釋放內存,請稍后!");
??? int i;
??? for (i = 0; i < rowSize; i++)
??? {
??????? //通過這種方式釋放內存的時候較慢,因為是一行行的釋放的
??????? free(pp[i]);
??? }
??? free(pp);
??? flag = 0;
}
5.編寫DDSS.c
#define _CRT_SECURE_NO_WARNINGS
#include "head.h"
?
extern int flag;
extern char **pp;
?
/************************************************************************/
/* 當當用戶信息查詢系統????????????????????????????????????????????????*/
/************************************************************************/
int main(int argc, char *argv[])
{
??? //文件所在位置
??? char *path = "G:\\dangdangwang.txt";
??? int fileSize = getFileSize(path);
??? //printf("%d字節,%fK,%fM", fileSize, fileSize /1024.0, fileSize / 1024.0 / 1024.0);
??? //這個選擇菜單是的字符串
??? char choice[25];
?
??? //獲得行號
??? int row = getFileRow(path);
??? printf("%d\n", row);
?
flag:system("cls");
??? main_view();
??? if (flag)
??? {
??????? printf("文件加載完畢,可以進行查找了!\n");
??? }
??? else if (flag == 0)
??? {
??????? printf("文件未加載狀態,請您先加載文件!\n");
??? }
??? while (1)
??? {
??????? printf("輸入內容或選擇('-v'顯示菜單):");
??????? scanf("%24s", choice);
?
??????? if (!strcmp(choice, "-v") || !strcmp(choice, "-view")){
??????????? main_view();
??????? }
??????? else if (!strcmp(choice, "-l") || !strcmp(choice, "-loadFile"))
??????? {
??????????? loadFile(path, row);
??????? }
??????? else if (!strcmp(choice, "-s") || !strcmp(choice, "-search"))
??????? {
??????????? while (1)
??????????? {
??????????????? char keyword[100] = { 0 };
??????????????? printf("\n請輸入要查找的字符串,輸入-exit將退出查找\n");
??????????????? scanf("%s", keyword);
?
??????????????? if (strcmp(keyword, "-exit"))
??????????????? {
??????????????????? findStringByKeyword(keyword, row);
??????????????? }
??????????????? else
??????????????? {
??????????????????? break;
??????????????? }
??????????? }
??????? }
??????? else if (!strcmp(choice, "-f") || !strcmp(choice, "-free"))
??????? {
??????????? freeMemory(pp, row);
??????? }
??????? goto flag;
??? }
?
??? system("pause");
??? return 0;
}
?
?
?
?
將查詢到的結果封裝到txt文本中
#define _CRT_SECURE_NO_WARNINGS?? //關閉安全檢查
#include <stdio.h>
#include <stdlib.h>
?
void main()
{
??? char *path = "G:\\dangdangwang.txt";
??? char *respath = "G:\\1.txt";
?
??? FILE *pf;
??? FILE *fp;
??? pf = fopen(path,"r");//讀的模式
??? fp = fopen(respath,"w");//寫的模式
??? if (pf == NULL)
??? {
??????? printf("文件打開失敗");
??? }
??? else
??? {
??????? //文件指針到末尾
??????? fseek(pf, 0, SEEK_END);
??????? int num = ftell(pf);
??????? char? strinfo[200];
??????? sprintf(strinfo, "\nnum=%d字節,%fK,%fM", num, num / 1024.0, num / 1024.0 / 1024.0);
??????? fputs(strinfo, fp);//寫入文件
??????? rewind(pf);//回到文件開頭
??????? while (!feof(pf))
??????? {
??????????? //緩沖區
??????????? char str[200];
??????????? //讀取,按照行讀取
??????????? fgets(str, 200, pf);
??????????? if (strstr(str,"譚勝") != NULL)? //字符串查找
??????????? {
??????????????? fputs(str, fp);//寫入文件
??????????????? printf("\n%s", str);//打印結果
??????????? }
??????? }
??????? fclose(fp);?? //關閉文件
??????? fclose(pf);?? //關閉文件
??? }
??? //打開結果文件
??? system(respath);
??? system("pause");
}
?
總結
以上是生活随笔為你收集整理的单线程实现检索当当网泄露的1GB用户数据的全部內容,希望文章能夠幫你解決所遇到的問題。