• 创建全局的二级指针

     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 + );//检索
}

最新文章

  1. js原声快速实现选项卡
  2. Oracle 12c In Memory Option初探
  3. 临时解决系统中大量的TIME_WAIT连接
  4. phpcms使用细节
  5. [转]inux之touch命令
  6. Codeforces Round #306 (Div. 2) C. Divisibility by Eight 暴力
  7. python学习笔记4(列表)
  8. HDU-3790 最短路径问题
  9. 获取contenteditable的内容 对html进行处理 兼容 chrome、IE、Firefox
  10. HTML5 Web存储(Web Storage)技术及用法
  11. asp.net mvc3 的数据验证(一)
  12. (详细)华为Mate7 MT7-TL00的usb调试模式在哪里开启的步骤
  13. 10行代码使用python统计词频
  14. 后端python基础
  15. 牛客练习赛14A(唯一分解定理)
  16. windows 64位 下 安装 tomcat
  17. Eclipse查看.properties文件中文乱码
  18. nRF52832 BLE_DFU空中升级OTA(一)安装软件(SDK14.2.0)
  19. Android基础总结(四)网络通信
  20. html禁止选中文字

热门文章

  1. openSUSE leap 42.3 添加HP Laserjet Pro M128fn打印机和驱动
  2. Django_模型操作
  3. 【Henu ACM Round#14 C】Duff and Weight Lifting
  4. [Python] for.. not in.. Remove Deduplication
  5. UDP连接调用connect()函数
  6. Timus 1935. Tears of Drowned 具体解释
  7. Markdown编辑器为什么好用以及好用的markdown编辑器
  8. js设计模式--------基本概念的理解
  9. Fragment-Transaction 源码分析
  10. XSY3244 10.31 D