博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

一道程序debug的题目

Posted on 2012-03-02 14:12  wangwangkunkun  阅读(151)  评论(0编辑  收藏  举报

这是一个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判断是否为空,很可能没有权限读。