• 创建全局的二级指针

     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查询的更多相关文章

  1. Redis进阶实践之十八 使用管道模式提高Redis查询的速度

    原文:Redis进阶实践之十八 使用管道模式提高Redis查询的速度 一.引言             学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中 ...

  2. java运行时内存模式学习

    学习java运行时内存模式: 各区介绍: 方法区(线程共享):用于存放被虚拟机加载的类的元数据:静态变量,常量,以及编译和的代码(字节码),也称为永久代(所有该类的实例被回收,或者此类classLoa ...

  3. C++(十八) — 内存模式、堆和栈

    1.内存模式 一个程序执行时,先复制到内存,然后CPU逐句读取指令执行. 每个存储单元存放一个字节(8bit)数据,每个有一个唯一的地址,地址是顺序编码的.比如:一台计算机256MB内存,则有256* ...

  4. Redis进阶实践之十八 使用管道模式加速Redis查询

    一.引言             学习redis 也有一段时间了,该接触的也差不多了.后来有一天,以为同事问我,如何向redis中批量的增加数据,肯定是大批量的,为了这主题,我从新找起了解决方案.目前 ...

  5. apache中配置php支持模块模式、cgi模式和fastcgi模式

    首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...

  6. apache中配置php支持模块模式、cgi模式和fastcgi模式的实验

    首先安装apache.mysql和php,依次顺序安装. 1.apache.mysql的安装比较简单,略过 2. php的安装,我安装的是php5.3.6内置了php-fpm,所以不需要再单独下补丁了 ...

  7. Angular JS 学习笔记(自定义服务:factory,Promise 模式异步请求查询:$http,过滤器用法filter,指令:directive)

    刚学没多久,作了一个小项目APP,微信企业号开发与微信服务号的开发,使用的是AngularJS开发,目前项目1.0版本已经完结,但是项目纯粹为了赶工,并没有发挥AngularJS的最大作用,这几天项目 ...

  8. jvm运行时内存模式

    jvm内存模型 内存模型粗略划分为:堆和栈 详细划分为:堆,虚拟机栈,方法区,本地方法区,程序计数器 程序计数器: 为了线程切换后能恢复到正确的执行位置,每条线程都需要有一个独立的程序计数器,各条线程 ...

  9. Java内存模式

    Java内存模型即Java Memory Model,简称JMM.JMM定义了Java 虚拟机(JVM)在计算机内存(RAM)中的工作方式. JVM是Java Virtual Machine(Java ...

随机推荐

  1. P1328 生活大爆炸版石头剪刀布

    题目描述 石头剪刀布是常见的猜拳游戏:石头胜剪刀,剪刀胜布,布胜石头.如果两个人出拳一样,则不分胜负.在<生活大爆炸>第二季第8 集中出现了一种石头剪刀布的升级版游戏. 升级版游戏在传统的 ...

  2. SqlCommand的四大方法

    SqlCommand类的方法 ---->>>1.ExecuteNonQuery(); 它的返回值类型为int型.多用于执行增加,删除,修改数据,返回受影响的行数.当select操作时 ...

  3. startActivityForResult and onActivityResult

    startActivityForResult and onActivityResult startActivityForResult 开启Activity 组织数据之后 发送,onActivityRe ...

  4. uni-app 地图初用 map

    一.uni-app 地图初用 map 代码如下: <template> <view> <!-- <page-head :title="title" ...

  5. Linux登陆类型-Linux中如何临时配置IP

    Linux登录: 本地登录,直接在Linux主机上接上键盘显示器,然后输入用户名密码登录 远程登录,通过网络进行登录(需要IP 账户名 密码) windows中远程登录软件有 xshell.putty ...

  6. rev---将文件中的每行内容以字符为单位反序输出

    rev命令将文件中的每行内容以字符为单位反序输出,即第一个字符最后输出,最后一个字符最先输出,依次类推.

  7. 比JLRoutes更强大更好用的iOS开源路由框架—FFRouter

    目前iOS常用路由框架是JLRouter.HHRouter.MGJRouter. 但是这些路由库都各有不足,首先是JLRouter,用不到的功能繁多,而且基于遍历查找URL,效率低下.HHRouter ...

  8. C++11 volatile 类型

    volatile作用: 作为指令关键字,确保本条指令不会受到编译器的优化而省略,而且要求每次直接读值. 定义: volatile int nTest; volatile关键字是一种类型修饰符,用它声明 ...

  9. sdut 2805(最小生成树)

    大家快来A水题 Time Limit: 1000MS Memory limit: 65536K 题目描写叙述 (1<= N <=2000)(1<= M <= N*(N-1)/2 ...

  10. [log4j]Slf4j的包冲突

    Caused by: java.lang.NoClassDefFoundError: Could not initialize class org.apache.log4j.Log4jLoggerFa ...