【发布时间】:2016-06-05 21:55:28
【问题描述】:
我正在编写一个简单的 HTTP 服务器,当文件存在时,我得到一个文件不存在的返回值
printf("%s\n", html_path);
if ((fd = open(html_path, "r")) >= 0){ //file found
stat(file_name, &st);
file_size = st.st_size;
printf("%d\n", file_size);
while (read(fd, response, RCVBUFSIZE) > 0){
}
}
else { //file not found
strcpy(response, "404 File not found\n");
send(clntSocket, response, 32, 0);
}
print语句是用来验证路径的,看起来是这样的:
/mounts/u-zon-d2/ugrad/kmwe236/HTML/index.html
请注意,此路径位于我们大学使用的服务器上。这是我命令pwd时显示的路径@
我已确认该文件存在。我的路径有问题吗?
【问题讨论】:
-
观察到的第一件事:有没有可能混合使用 open 和 fopen? "r" 适合 fopen,因为 open 你会通过 O_RDONLY。
-
你从哪里得到
open函数?如果这应该是一个系统调用open(2),那么它有不同的原型,你应该写open(html_path, O_RDONLY)。或者,也许您想使用fopen(3)?无论如何,如果失败并不意味着该文件不存在:可能还有其他问题,例如您没有适当的访问权限。使用strerror(3)打印错误的实际原因。
标签: c file file-exists