高分求解,CGI程序怎么访问服务器的本地文件

zllzmcDCR 2006-10-23 03:01:55
在c程序里面有这样一个方法:

int log( char *input ) {
FILE * pFile;
pFile = fopen ("log.txt" , "w");
fwrite (input , 1 , strlen( input ) +1 , pFile);
fclose (pFile);
return 0;
}

该C程序编译后生成test.exe,然后改名为test.cgi。copy到IIS发布目录,C:\Inetpub\wwwroot\cgi\testDic\sample,该目录里面有log.txt文件,
如果不调用log("XXXX")方法,test.cgi能产生正确结果;一旦调用该方
法,HTML页面就返回出错信息:
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

无奈了,难道IIS连pFile = fopen ("log.txt" , "w")这种相对路径都不
认识么。

求解,我该怎么做才能访问到同一目录下的log.txt 。
...全文
388 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
hu_zy 2006-12-28
  • 打赏
  • 举报
回复
你这个错误 不是打开文件失败的问题,而是你没有返回http头!

在最开始的时候加上:
printf("Content-type: text/html\n\n");

cloudgamer 2006-12-08
  • 打赏
  • 举报
回复
权限
newzy 2006-10-27
  • 打赏
  • 举报
回复
根本原因不是因为存取路径, 而是因为权限.

看下这三行:
FILE * pFile;
pFile = fopen ("C:\\Inetpub\\wwwroot\\cgi\\testDic\\sample\\log.txt" , "w");
fwrite (input , 1 , strlen( input ) +1 , pFile);

如果此目录没有写权限, 会怎么?
pFile 会等于 NULL, 程序必然会发生异常.

结果呢, MIMI 头部将不能被正确地输出.
必然打印:
CGI Error
The specified CGI application misbehaved by not returning a complete set of HTTP headers. The headers it did return are:

zzzaaaxq 2006-10-26
  • 打赏
  • 举报
回复

呵呵 这个问题我以前也曾被困扰过

1. 凡属 cgi程序存取的文件, 统一采取 cgi程序所在位置的相对路径

2. 凡属 url链接访问的文件, 统一采取 index.htm所在位置的相对路径

请按以上的办法试一下应该可以通过

luck you!

zllzmcDCR 2006-10-23
  • 打赏
  • 举报
回复
所有代码我也发一下吧

#include <mecab.h>
#include <stdio.h>

#define CHECK(eval) if (! eval) { \
fprintf (stderr, "Exception:%s\n", mecab_strerror (mecab)); \
mecab_destroy(mecab); \
return -1; }

int log( char *input ) {
FILE * pFile;
pFile = fopen ("C:\\Inetpub\\wwwroot\\cgi\\testDic\\sample\\log.txt" , "w");
fwrite (input , 1 , strlen( input ) +1 , pFile);
fclose (pFile);
return 0;
}

int main (int argc, char **argv)
{
log( " 1" );

printf("Content-type: text/html\n\n");
printf("<HTML><BODY>");
printf("<kanji>\n");
printf("<item>\n");
printf("<value>");
printf("</value>\n");
printf("</item>\n");
printf("</kanji>\n");
printf("</BODY></HTML>");

return 0;
}
zllzmcDCR 2006-10-23
  • 打赏
  • 举报
回复
这样子改成绝对路径还是不行。

int log( char *input ) {
FILE * pFile;
pFile = fopen ("C:\\Inetpub\\wwwroot\\cgi\\testDic\\sample\\log.txt" , "w");
fwrite (input , 1 , strlen( input ) +1 , pFile);
fclose (pFile);
return 0;
}

出同样的错误。
yigerenzhu 2006-10-23
  • 打赏
  • 举报
回复
换成绝对路径用一下,就应该可以了

2,204

社区成员

发帖
与我相关
我的任务
社区描述
Web 开发 CGI
社区管理员
  • CGI社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧