我使用.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
}
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;
}
}
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);
}
*
*
[ValueList]
x = 100;
y = 70;
当通过html页面发送请求时,我总是丢失.ini文件。如果直接从C文件发送请求,它就可以正常工作。
如何解决这个问题?
答案 0 :(得分:0)
也许您对URL参数的编码有问题?您不能只通过URL传递任意字符串 - 有些字符必须编码。阅读此页面关于URL encoding。
在C程序中显示data
字符串的值对解决问题非常有帮助。
<强>更新强>
当您的程序在Web服务器调用或您直接调用时执行的位置可能会有所不同。你确定它是用相同的“当前目录”执行的。机会是不同的,因此当您尝试打开ini文件时,您将失败。尝试打印出当前目录(即使用getcwd
功能)并比较两种情况。