- 创建全局的二级指针
1 char ** g_pp;//全局的二级指针
- 获取数据有多少行
1 //获取行数 2 int getimax() 3 { 4 int hang = -1; 5 FILE *pf = fopen(path, "r");//读文件打开路径 6 if (pf == NULL) 7 { 8 printf("文件打开失败"); 9 return -1; 10 } 11 else 12 { 13 hang = 0; 14 while (!feof(pf))//到了文件末尾返回1,没有返回0 15 { 16 char readstr[1024] = { 0 }; 17 18 fgets(readstr, 1024, pf);//读取一行 19 20 hang++;//自增 21 22 } 23 fclose(pf);//关闭 24 return hang; 25 } 26 }
- 定义行数
1 int imax = 16151574;//标示有多少行
- 载入内存
1 void loadfromfile() 2 { 3 4 g_pp = (char **)malloc(sizeof(char*)*imax); //分配指针数组 5 memset(g_pp, ‘\0‘, sizeof(char*)*imax);//内存清零 6 7 8 FILE *pf = fopen(path, "r");//读文件打开路径 9 if (pf == NULL) 10 { 11 printf("文件打开失败"); 12 return -1; 13 } 14 else 15 { 16 for (int i = 0; i < imax; i++) 17 { 18 char str[1024] = { 0 }; 19 fgets(str, 1024, pf);//按行读取 20 str[1024 - 1] = ‘\0‘; 21 int strlength = strlen(str); 22 23 24 g_pp[i] = malloc(sizeof(char)*(strlength + 1));//处理/0 25 26 if (g_pp[i] != NULL) 27 { 28 strcpy(g_pp[i], str);//拷贝到分配的内存 29 } 30 } 31 fclose(pf);//关闭 32 } 33 }
- 查询并写入到文件
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); }
- main
1 loadfromfile(); 2 printf("Content-type:text/html\n\n");//换行 3 4 system("mkdir 1"); 5 6 char szpost[256] = { 0 }; 7 gets(szpost); 8 printf("%s", szpost); 9 10 char*p1 = strchr(szpost, ‘&‘); 11 if (p1 != NULL) 12 { 13 *p1 = ‘\0‘; 14 } 15 printf("<br>%s", szpost + 5); 16 printf("<br>%s", change(szpost + 5)); 17 18 char *p2 = strchr(p1 + 1, ‘&‘); 19 if (p2 != NULL) 20 { 21 *p2 = ‘\0‘; 22 } 23 printf("<br>%s", p1 + 6); 24 printf("<br>%s", change(p1 + 6)); 25 26 search(szpost + 5);//检索
- cgi格式转换
1 char* change(char *str) 2 { 3 char *tempstr = malloc(strlen(str) + 1); 4 int x = 0, y = 0; 5 char assii_1, assii_2; 6 while (tempstr[x]) 7 { 8 if ((tempstr[x] = str[y]) == ‘%‘) 9 { 10 //y+1 y+2 11 if (str[y + 1] >= ‘A‘) 12 { 13 assii_1 = str[y + 1] - 55; 14 15 } 16 else 17 { 18 assii_1 = str[y + 1] - 48; 19 } 20 if (str[y + 2] >= ‘A‘) 21 { 22 assii_2 = str[y + 2] - 55; 23 } 24 else 25 { 26 assii_2 = str[y + 2] - 48; 27 } 28 tempstr[x] = assii_1 * 16 + assii_2; 29 30 y += 2; 31 32 } 33 x++; 34 y++; 35 } 36 tempstr[x] = ‘\0‘; 37 38 return tempstr; 39 }
完整代码
1 #define _CRT_SECURE_NO_WARNINGS 2 #include <stdio.h> 3 #include <stdlib.h> 4 #include <string.h> 5 #include<memory.h> 6 #include <Windows.h> 7 #define path "kaifang.txt" 8 9 10 11 12 13 char ** g_pp;//全局的二级指针 14 int imax = 16151574;//标示有多少行 15 16 //获取行数 17 int getimax() 18 { 19 int hang = -1; 20 FILE *pf = fopen(path, "r");//读文件打开路径 21 if (pf == NULL) 22 { 23 printf("文件打开失败"); 24 return -1; 25 } 26 else 27 { 28 hang = 0; 29 while (!feof(pf))//到了文件末尾返回1,没有返回0 30 { 31 char readstr[1024] = { 0 }; 32 33 fgets(readstr, 1024, pf);//读取一行 34 35 hang++;//自增 36 37 } 38 fclose(pf);//关闭 39 return hang; 40 } 41 } 42 43 char* change(char *str) 44 { 45 char *tempstr = malloc(strlen(str) + 1); 46 int x = 0, y = 0; 47 char assii_1, assii_2; 48 while (tempstr[x]) 49 { 50 if ((tempstr[x] = str[y]) == ‘%‘) 51 { 52 //y+1 y+2 53 if (str[y + 1] >= ‘A‘) 54 { 55 assii_1 = str[y + 1] - 55; 56 57 } 58 else 59 { 60 assii_1 = str[y + 1] - 48; 61 } 62 if (str[y + 2] >= ‘A‘) 63 { 64 assii_2 = str[y + 2] - 55; 65 } 66 else 67 { 68 assii_2 = str[y + 2] - 48; 69 } 70 tempstr[x] = assii_1 * 16 + assii_2; 71 72 y += 2; 73 74 } 75 x++; 76 y++; 77 } 78 tempstr[x] = ‘\0‘; 79 80 return tempstr; 81 } 82 83 void loadfromfile() 84 { 85 86 g_pp = (char **)malloc(sizeof(char*)*imax); //分配指针数组 87 memset(g_pp, ‘\0‘, sizeof(char*)*imax);//内存清零 88 89 90 FILE *pf = fopen(path, "r");//读文件打开路径 91 if (pf == NULL) 92 { 93 printf("文件打开失败"); 94 return -1; 95 } 96 else 97 { 98 for (int i = 0; i < imax; i++) 99 { 100 char str[1024] = { 0 }; 101 fgets(str, 1024, pf);//按行读取 102 str[1024 - 1] = ‘\0‘; 103 int strlength = strlen(str); 104 105 106 g_pp[i] = malloc(sizeof(char)*(strlength + 1));//处理/0 107 108 if (g_pp[i] != NULL) 109 { 110 strcpy(g_pp[i], str);//拷贝到分配的内存 111 } 112 } 113 fclose(pf);//关闭 114 } 115 } 116 117 void search(char *str) 118 { 119 char strpath[100] = { 0 }; 120 sprintf(strpath, "I:\\%s.txt", str); 121 FILE *pf = fopen(strpath, "w");//写的模式打开 122 123 if (g_pp != NULL) 124 { 125 126 for (int i = 0; i < imax; i++) 127 { 128 if (g_pp[i] != NULL) 129 { 130 char *p = strstr(g_pp[i], str);//找到返回地址,找不到返回null 131 if (p != NULL) 132 { 133 puts(g_pp[i]);//打印 134 fputs(g_pp[i], pf);//输出到文件 135 } 136 } 137 } 138 } 139 fclose(pf); 140 } 141 142 143 144 void main() 145 { 146 147 loadfromfile(); 148 printf("Content-type:text/html\n\n");//换行 149 150 system("mkdir 1"); 151 152 char szpost[256] = { 0 }; 153 gets(szpost); 154 printf("%s", szpost); 155 156 char*p1 = strchr(szpost, ‘&‘); 157 if (p1 != NULL) 158 { 159 *p1 = ‘\0‘; 160 } 161 printf("<br>%s", szpost + 5); 162 printf("<br>%s", change(szpost + 5)); 163 164 char *p2 = strchr(p1 + 1, ‘&‘); 165 if (p2 != NULL) 166 { 167 *p2 = ‘\0‘; 168 } 169 printf("<br>%s", p1 + 6); 170 printf("<br>%s", change(p1 + 6)); 171 172 search(szpost + 5);//检索 173 }