您的位置:首页 > 其它

一道程序debug的题目

2012-03-02 14:12 85 查看
这是一个download file 的 c写的cgi ,请找出程序错误和可能有问题的地方。

void downFile( char *filename)
{

char buff[655];
int n;
time_t tp;
FILE *fp;
struct stat s;
tp=time(NULL);
stat(filename, &s);
printf("HTTP/1.1 200 OK\n");
printf("Content-Disposition: attachment;filename=test.txt\n");
printf("Content-Transfer-Encoding: binary\n");
printf("Accept-Ranges: bytes\n");
printf("Content-Length: %ld\n",s.st_size);
printf("Connection: close\n");
printf("Content-type: application/octet-stream\n");

fp=fopen(filename, "rb");
while((n=fread(&buff, sizeof(char),655,fp))>0){
fwrite(buff, 1, n, stdout);
}
fclose(fp);

}

答案:

1,windows 的话 注意 \r \n

2,Content-type: 后面是\n\n不是\n

3,&buff这错误。

4,fp判断是否为空,很可能没有权限读。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: