【发布时间】:2010-11-30 03:31:03
【问题描述】:
我正在尝试用 c++ 编写一个 CGI 脚本,它打印反向网络路径(使用 traceroute) 从 Web 服务器到调用 CGI 脚本的客户端的 IP 地址。
当我在 Visual Studio 中运行程序时,它工作正常(创建进程,将结果打印到“C:/result.out”文件中,打开文件,打印文件中的每一行,关闭文件)但是在编译后并尝试仅运行其 .exe 文件,它会引发异常。我该怎么做才能使 .exe 正常工作? 请注意,我使用的是 Windows XP 和 Visual C++ 2008
代码如下:
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <process.h>
#include <conio.h>
int main()
{
char *line, *command, *userIp;
printf("Content-Type:text/html\n\n");
printf("<html><head></head><br/>");
printf("<body><br/>");
line = (char*)malloc(255*sizeof(char));
command = (char*)malloc(10*sizeof(char));
userIp = (char*)malloc(30*sizeof(char));
//userIp = getenv("REMOTE_ADDR"); // use a default IP until program works
strcpy(command,"tracert ");
strcpy(userIp,"74.125.87.104");
strcat(command,userIp);
strcat(command," > C:/result.out");
// create command "tracert 74.125.87.104 > C:/result.out"
printf("%s",command);
system(command);
FILE *f;
f = fopen("C:/result.out","r"); // open C:/result.out and read line - by - line
strcpy(line,"");
while(!feof(f)){
fgets(line,255,f);
printf("%s\n",line);
}
fclose(f);
printf("<br/>Test running OK<br/>");
printf("</body></html>");
getch();
return 0;
}
【问题讨论】:
-
请问它抛出的异常是什么?
-
I
m sorry, Im 不太擅长 C 语言中的异常处理,所以我无法弄清楚。它只提示我我的程序抛出了一个未处理的 win32 异常。尽管如此,我还是设法解决了我的问题。不过谢谢你的回复