81.内存模式实现cgi查询
- 创建全局的二级指针
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 + );//检索
}
81.内存模式实现cgi查询的更多相关文章
- Redis进阶实践之十八 使用管道模式提高Redis查询的速度
原文:Redis进阶实践之十八 使用管道模式提高Redis查询的速度 一.引言 学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中 ...
- java运行时内存模式学习
学习java运行时内存模式: 各区介绍: 方法区(线程共享):用于存放被虚拟机加载的类的元数据:静态变量,常量,以及编译和的代码(字节码),也称为永久代(所有该类的实例被回收,或者此类classLoa ...
- C++(十八) — 内存模式、堆和栈
1.内存模式 一个程序执行时,先复制到内存,然后CPU逐句读取指令执行. 每个存储单元存放一个字节(8bit)数据,每个有一个唯一的地址,地址是顺序编码的.比如:一台计算机256MB内存,则有256* ...
- Redis进阶实践之十八 使用管道模式加速Redis查询
一.引言 学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前 ...
- apache中配置php支持模块模式、cgi模式和fastcgi模式
首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...
- apache中配置php支持模块模式、cgi模式和fastcgi模式的实验
首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...
- Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)
刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...
- jvm运行时内存模式
jvm内存模型 内存模型粗略划分为:堆和栈 详细划分为:堆,虚拟机栈,方法区,本地方法区,程序计数器 程序计数器: 为了线程切换后能恢复到正确的执行位置,每条线程都需要有一个独立的程序计数器,各条线程 ...
- Java内存模式
Java内存模型即Java Memory Model,简称JMM.JMM定义了Java 虚拟机(JVM)在计算机内存(RAM)中的工作方式. JVM是Java Virtual Machine(Java ...
随机推荐
- ivms4200 远程桌面访问测试过程及问题汇总
17.11.4 测试存储服务器配置后能否自动录像确认 10:34 4200客户端关闭 10:40 打开4200客户端软件 10:51 关机 10:56 开机,有提示出现,“防火墙阻止... ...
- 实现人脸识别性别之路---网页上的video标签
<video> 元素支持三种视频格式: MP4, WebM, 和 Ogg.但是,不同的浏览器对视频格式的支持也不一致,因此为了让浏览器都适应,我们使用source属性来对视频文件格式定义 ...
- java.sql.SQLException:错误 The server time zone value 'Öйú±ê׼ʱ¼ä' is unrecognized or represents more than one time zone.
错误 方法 添加后面的内容即可
- strings---对象文件或二进制文件中查找可打印的字符串
strings命令在对象文件或二进制文件中查找可打印的字符串.字符串是4个或更多可打印字符的任意序列,以换行符或空字符结束. strings命令对识别随机对象文件很有用. 语法 strings [ - ...
- 五十个UI设计资源网站
五十个UI设计资源网站 用户体验团队网站 1.UCD大社区 http://ucdchina.com/ 2.腾讯WSD http://wsd.tencent.com/ 3.腾讯CDC http://cd ...
- hdu 5312 Sequence(数学推导——三角形数)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5312 Sequence Time Limit: 2000/2000 MS (Java/Others) ...
- View State
如何查看viewstate 鼠标右键页面,然后view page source 源码中搜索viewstate,会找到一个隐藏的字段. <input type="hidden" ...
- Python: PS 滤镜特效 -- Marble Filter
本文用 Python 实现 PS 滤镜特效,Marble Filter, 这种滤镜使图像产生不规则的扭曲,看起来像某种玻璃条纹, 具体的代码如下: import numpy as np import ...
- SpringBoot与Dubbo的整合-zookeeper和监控中心搭建
对于Dubbo的应用已经是十分普遍,自从阿里巴巴开源以来,国内许多公司就采用了dubbo的架构来开发项目.不过再dubbo十分火的时候,突然就停止更新了, 只有当当网还在其基础进行了拓展(dubbox ...
- html ---- a 标签 在新窗口打开的问题