1

我正在使用 .ini 文件来存储一些值并使用 iniparser 从中检索值。

当我给出(硬编码)查询并通过命令行检索值时,我能够检索 ini 文件并执行一些操作。

但是当我通过http传递查询时,我得到一个错误(找不到文件),即无法加载ini文件。


  • 命令行 :

int main(void)
{
   printf("Content-type: text/html; charset=utf-8\n\n");

   char* data = "/cgi-bin/set.cgi?pname=x&value=700&url=http://IP/home.html";

   //perform some operation
}

  • 通过http:

.html

function SetValue(id)
{
    var val;
    var URL = window.location.href;
    if(id =="set")
    {
        document.location = "/cgi-bin/set.cgi?pname="+rwparams+"&value="+val+"&url="+URL;
    }
}

  • 。C

int * Value(char* pname)
{
    dictionary * ini ;
    char *key1 = NULL;
    char *key2 =NULL;
    int i =0;

    int val;

    ini = iniparser_load("file.ini");
    if(ini != NULL)
    {
        //key for fetching the value
        key1 = (char*)malloc(sizeof(char)*50);
        if(key1 != NULL)
        {                   
                strcpy(key1,"ValueList:");
                key2 = (char*)malloc(sizeof(char)*50);
                if(key2 != NULL)
                {
                    strcpy(key2,pname);
                    strcat(key1,key2);                  
                    val = iniparser_getint(ini, key1, -1);
                    if(-1 == val || 0 > val)
                    {
                        return 0;                       
                    }
                }
                else
                {
                    //error
                    free(key1);                     
                    return;
                }           
        }       
        else
        {   
            printf("ERROR : Memory Allocation Failure ");
            return;
        }

    }
    else
    {
        printf("ERROR : .ini File Missing");
        return;
    }
    iniparser_freedict(ini);
    free(key1);
    free(key2);
    return (int *)val;
}

void get_Value(char* pname,char* value)
{
        int result =0;                          
        result = Value(pname);
        printf("Result : %d",result);           
}

int main(void)
{
    printf("Content-type: text/html; charset=utf-8\n\n");

    char* data = getenv("QUERY_STRING");    
    //char* data = "/cgi-bin/set.cgi?pname=x&value=700&url=http://10.50.25.40/home.html";

    //Parse to get the values seperately as parameter name, parameter value, url

    //Calling get_Value method to set the value
    get_Value(final_para,final_val);

}

*

  • 文件.ini

*

[ValueList]

x   = 100;
y   = 70;

当请求通过 html 页面发送时,我总是丢失 .ini 文件。如果直接从 C 文件发送请求,则它们可以正常工作。

如何解决这个问题?

4

1 回答 1

0

也许您对 URL 参数的编码有疑问?您不能只通过 URL 传递任意字符串 - 有些字符必须进行编码。阅读有关URL 编码的页面。

在您的 C 程序中显示字符串的值data可能对解决您的问题有很大帮助。


更新:

当您的程序被 Web 服务器调用或直接由您调用时,您的程序执行的位置可能会有所不同。您确定它正在使用相同的“当前目录”执行。可能是不同的,因此当您尝试打开 ini 文件时会失败。尝试打印出当前目录(即使用getcwd函数)并比较两种情况。

于 2010-01-28T07:32:39.390 回答