81.内存模式实现cgi查询
2024-09-02 00:35:37
- 创建全局的二级指针
char ** g_pp;//全局的二级指针
- 获取数据有多少行
//获取行数
int getimax()
{
int hang = -;
FILE *pf = fopen(path, "r");//读文件打开路径
if (pf == NULL)
{
printf("文件打开失败");
return -;
}
else
{
hang = ;
while (!feof(pf))//到了文件末尾返回1,没有返回0
{
char readstr[] = { }; fgets(readstr, , pf);//读取一行 hang++;//自增 }
fclose(pf);//关闭
return hang;
}
} - 定义行数
int imax = ;//标示有多少行
- 载入内存
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 = ; i < imax; i++)
{
char str[] = { };
fgets(str, , pf);//按行读取
str[ - ] = '\0';
int strlength = strlen(str); g_pp[i] = malloc(sizeof(char)*(strlength + ));//处理/0 if (g_pp[i] != NULL)
{
strcpy(g_pp[i], str);//拷贝到分配的内存
}
}
fclose(pf);//关闭
}
} - 查询并写入到文件
void search(char *str)
{
char strpath[] = { };
sprintf(strpath, "I:\\%s.txt", str);
FILE *pf = fopen(strpath, "w");//写的模式打开 if (g_pp != NULL)
{ for (int i = ; 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
loadfromfile();
printf("Content-type:text/html\n\n");//换行 system("mkdir 1"); char szpost[] = { };
gets(szpost);
printf("%s", szpost); char*p1 = strchr(szpost, '&');
if (p1 != NULL)
{
*p1 = '\0';
}
printf("<br>%s", szpost + );
printf("<br>%s", change(szpost + )); char *p2 = strchr(p1 + , '&');
if (p2 != NULL)
{
*p2 = '\0';
}
printf("<br>%s", p1 + );
printf("<br>%s", change(p1 + )); search(szpost + );//检索 - cgi格式转换
char* change(char *str)
{
char *tempstr = malloc(strlen(str) + );
int x = , y = ;
char assii_1, assii_2;
while (tempstr[x])
{
if ((tempstr[x] = str[y]) == '%')
{
//y+1 y+2
if (str[y + ] >= 'A')
{
assii_1 = str[y + ] - ; }
else
{
assii_1 = str[y + ] - ;
}
if (str[y + ] >= 'A')
{
assii_2 = str[y + ] - ;
}
else
{
assii_2 = str[y + ] - ;
}
tempstr[x] = assii_1 * + assii_2; y += ; }
x++;
y++;
}
tempstr[x] = '\0'; return tempstr;
}
完整代码
#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 = ;//标示有多少行 //获取行数
int getimax()
{
int hang = -;
FILE *pf = fopen(path, "r");//读文件打开路径
if (pf == NULL)
{
printf("文件打开失败");
return -;
}
else
{
hang = ;
while (!feof(pf))//到了文件末尾返回1,没有返回0
{
char readstr[] = { }; fgets(readstr, , pf);//读取一行 hang++;//自增 }
fclose(pf);//关闭
return hang;
}
} char* change(char *str)
{
char *tempstr = malloc(strlen(str) + );
int x = , y = ;
char assii_1, assii_2;
while (tempstr[x])
{
if ((tempstr[x] = str[y]) == '%')
{
//y+1 y+2
if (str[y + ] >= 'A')
{
assii_1 = str[y + ] - ; }
else
{
assii_1 = str[y + ] - ;
}
if (str[y + ] >= 'A')
{
assii_2 = str[y + ] - ;
}
else
{
assii_2 = str[y + ] - ;
}
tempstr[x] = assii_1 * + assii_2; y += ; }
x++;
y++;
}
tempstr[x] = '\0'; return tempstr;
} 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 = ; i < imax; i++)
{
char str[] = { };
fgets(str, , pf);//按行读取
str[ - ] = '\0';
int strlength = strlen(str); g_pp[i] = malloc(sizeof(char)*(strlength + ));//处理/0 if (g_pp[i] != NULL)
{
strcpy(g_pp[i], str);//拷贝到分配的内存
}
}
fclose(pf);//关闭
}
} void search(char *str)
{
char strpath[] = { };
sprintf(strpath, "I:\\%s.txt", str);
FILE *pf = fopen(strpath, "w");//写的模式打开 if (g_pp != NULL)
{ for (int i = ; 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);
} void main()
{ loadfromfile();
printf("Content-type:text/html\n\n");//换行 system("mkdir 1"); char szpost[] = { };
gets(szpost);
printf("%s", szpost); char*p1 = strchr(szpost, '&');
if (p1 != NULL)
{
*p1 = '\0';
}
printf("<br>%s", szpost + );
printf("<br>%s", change(szpost + )); char *p2 = strchr(p1 + , '&');
if (p2 != NULL)
{
*p2 = '\0';
}
printf("<br>%s", p1 + );
printf("<br>%s", change(p1 + )); search(szpost + );//检索
}
最新文章
- js原声快速实现选项卡
- Oracle 12c In Memory Option初探
- 临时解决系统中大量的TIME_WAIT连接
- phpcms使用细节
- [转]inux之touch命令
- Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
- python学习笔记4(列表)
- HDU-3790 最短路径问题
- 获取contenteditable的内容 对html进行处理 兼容 chrome、IE、Firefox
- HTML5 Web存储(Web Storage)技术及用法
- asp.net mvc3 的数据验证(一)
- (详细)华为Mate7 MT7-TL00的usb调试模式在哪里开启的步骤
- 10行代码使用python统计词频
- 后端python基础
- 牛客练习赛14A(唯一分解定理)
- windows 64位 下 安装 tomcat
- Eclipse查看.properties文件中文乱码
- nRF52832 BLE_DFU空中升级OTA(一)安装软件(SDK14.2.0)
- Android基础总结(四)网络通信
- html禁止选中文字
热门文章
- openSUSE leap 42.3 添加HP Laserjet Pro M128fn打印机和驱动
- Django_模型操作
- 【Henu ACM Round#14 C】Duff and Weight Lifting
- [Python] for.. not in.. Remove Deduplication
- UDP连接调用connect()函数
- Timus 1935. Tears of Drowned 具体解释
- Markdown编辑器为什么好用以及好用的markdown编辑器
- js设计模式--------基本概念的理解
- Fragment-Transaction 源码分析
- XSY3244 10.31 D