找不到C文件
C file never found
我正在制作一个套接字服务器,例如使用 "localhost:8080/WWW/index.html" 之类的输入,它将 return index.html 文件。我能够解析请求,这样当有人输入时,我将有一个缓冲区“/WWW/index.html”,如果我输入 vim /WWW/index.html 我会打开,但由于某种原因我的代码总是说找不到该文件。下面是代码...
char* parseRequest(char* request) {
//assume file paths are no more than 256 bytes + 1 for null.
char *buffer = malloc(sizeof(char)*257);
memset(buffer, 0, 257);
if(fnmatch("GET * HTTP/1.*", request, 0)) return 0;
sscanf(request, "GET %s HTTP/1.", buffer);
return buffer;
}
int fileExists(const char *fname){
FILE *file;
if(file = fopen(fname, "r"))
{
fclose(file);
return 1;
}
return 0;
}
recv(sock,buffer,255,0);
//reading inputted directory
char *dir = parseRequest(buffer);
if(fileExists(dir) == 1){
send(sock, "File found", 200, 0);
}else{
send(sock, "404: File not found", 200, 0);
}
其中 dir
等于“/WWW/index.html”
#define MAXBUF 256
#define DOCUMENTS_ROOT "/home/longbear/hw4"
char path[MAXBUF] = DOCUMENTS_ROOT;
strcat(path, dir);
。
.
.
现在 您应该可以在 path
打开文件。
我正在制作一个套接字服务器,例如使用 "localhost:8080/WWW/index.html" 之类的输入,它将 return index.html 文件。我能够解析请求,这样当有人输入时,我将有一个缓冲区“/WWW/index.html”,如果我输入 vim /WWW/index.html 我会打开,但由于某种原因我的代码总是说找不到该文件。下面是代码...
char* parseRequest(char* request) {
//assume file paths are no more than 256 bytes + 1 for null.
char *buffer = malloc(sizeof(char)*257);
memset(buffer, 0, 257);
if(fnmatch("GET * HTTP/1.*", request, 0)) return 0;
sscanf(request, "GET %s HTTP/1.", buffer);
return buffer;
}
int fileExists(const char *fname){
FILE *file;
if(file = fopen(fname, "r"))
{
fclose(file);
return 1;
}
return 0;
}
recv(sock,buffer,255,0);
//reading inputted directory
char *dir = parseRequest(buffer);
if(fileExists(dir) == 1){
send(sock, "File found", 200, 0);
}else{
send(sock, "404: File not found", 200, 0);
}
其中 dir
等于“/WWW/index.html”
#define MAXBUF 256
#define DOCUMENTS_ROOT "/home/longbear/hw4"
char path[MAXBUF] = DOCUMENTS_ROOT;
strcat(path, dir);
。 . .
现在 您应该可以在 path
打开文件。