0
我如何strtok
文件名以便原始字符串不會受到影響?拆分字符串,不影響源
void generate_header(int sock, char* filename, int response_code) {
char buffer[BUFLEN];
// For Content-Type generation
char *file_format;
const char delimiter[2] = ".";
file_format = strtok(filename, delimiter);
file_format = strtok(NULL, delimiter);
// Generate response code
/*
........ ommited ..........
*/
// Generate Content-Type
if (strcmp(file_format, "html") == 0 || strcmp(file_format, "htm") == 0) {
strncat(buffer, "Content-Type: text/html\n", sizeof(buffer) - strlen(buffer) - 1);
}
else if (strcmp(file_format, "txt") == 0) {
strncat(buffer, "Content-Type: text/plain\n", sizeof(buffer) - strlen(buffer) - 1);
}
else if (strcmp(file_format, "jpg") == 0 || strcmp(file_format, "jpeg") == 0) {
strncat(buffer, "Content-Type: image/jpeg\n", sizeof(buffer) - strlen(buffer) - 1);
}
else if (strcmp(file_format, "gif") == 0) {
strncat(buffer, "Content-Type: image/gif\n", sizeof(buffer) - strlen(buffer) - 1);
} else {
strncat(buffer, "Content-Type: application/octet-stream\n", sizeof(buffer) - strlen(buffer) - 1);
}
// End
strncat(buffer, "Connection: close\n", sizeof(buffer) - strlen(buffer) - 1);
// Push
write(sock, buffer, strlen(buffer));
}
爲什麼有'C++'和'C'標籤?哪一個?他們是不一樣的?! – duedl0r
如果你使用C++:使用std :: string,在循環中使用find_first_of(),並放棄C的東西。 – PaulMcKenzie
此外,如果您的目標是獲取文件擴展名,是否沒有您應該使用的系統函數已經這樣做,沒有您使用strtok?大多數(如果不是全部)主要的C++(甚至是C)編譯器庫都帶有這樣的函數。 – PaulMcKenzie