我有一个字符串说
char resp_buf[20000];
sprintf(resp_buf,"HTTP/1.0 404 File not Found.\r\nContent-Type: text/html\r\\
n\r\n<html><body><h2>Error 404: The file your have requested does not exist.</h2\
></body></html>");
所以在此之后,当我稍后调用发送时-->
send(fd,resp_buf,strlen(a),0);
数据确实已发送,但服务器崩溃并出现段错误。
如何找到它的大小(以字节为单位),以便我可以在 send() 中使用它进行套接字编程。我收到了大量的段错误。我尝试过 strlen(a) 和 sizeof(a),但两者都给出了
Segmentation fault (core dumped)
最佳答案
int sprintf(char * restrict str, const char * restrict format, ...);
返回作为返回值打印的字节数,您可能想尝试使用它。
或者总是有的
size_t strlen(const char *s);
关于c - 如何计算c中字符串的字节大小,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18786034/