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 ...
随机推荐
- 基于CANopen DSP402的运动控制笔记
常用的mode of operation 有以下几种: 控制字 control word: 6--------------7---------------15--------------------7 ...
- 便捷编程-Xcode常用第三方插件 (随时更新)
Xcode工具插件 1.XAlign 让Xcode编辑器中的代码以多种方式瞬间对齐 地址:https://github.com/qfish/XAlign 2.VVDocumenter-Xcode 在X ...
- Dcloud+mui 压缩上传图片到服务器
chooseImgFromAlbums选择图片 chooseImgFromPictures 拍照 changeToLocalUrl 转换成可用的路径 uploadpic.compressImg 压缩图 ...
- LINQ to SQL活学活用(1):这要打破旧观念
程序架构 如今比較经典的架构,看看以下图片. 怎样实现 在一个N层应用程序中我们怎样使用LINQ to SQL呢?这给刚刚入门的朋友的确是个难题,使用LINQ to SQL就是ORM技术,能够非常轻松 ...
- PS实现分幅扫描图片的批量自己主动拼接
非常easy,仅仅需两步搞定: 一.打开工具.如图所看到的: 二.选择图片,进行拼接: 静待结果!
- DistBelief 框架下的并行随机梯度下降法 - Downpour SGD
本文是读完 Jeffrey Dean, Greg S. Corrado 等人的文章 Large Scale Distributed Deep Networks (2012) 后的一则读书笔记,重点 ...
- js设计模式--------基本概念的理解
1.闭包,前面已经说过,这里不再做说明 2.封装 对于JS而言,他不像java一样存在私有,公有 ,可以让对象在一些细节方面存在差异,降低他们的耦合程度,对数据做一些约束,我们可以更容易调试,封 ...
- 84.friend友元类
#include <iostream> using namespace std; //友元函数的主要作用就是访问私有变量 class myclass { public: friend cl ...
- Android eclipse 提示java代码 快捷键
1.提示java代码能够用ALT+/ 键就能够了(前提是你要把你须要的类或方法的首字母打出来).我添加的部分:仅仅要输入sysout,然后alt+/键就能够输出System.out.prinln(), ...
- 【OC学习-8】存取器方法?getter和setter?事实上就是赋值和返回值的两种函数
我们在声明类的时候,有实例变量+方法(函数),这些实例变量假设默认的话都是protected类型,一般无法直接訪问.更别提赋值和调用了,所以就产生了两种函数,getter函数就是可以返回实例变量的值, ...