本文介绍了strcat()问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有以下代码: char request [] =" GET"; strcat(请求,路径); // Path是url的路径部分ie。 " /path/test.htm" strcat(request," HTTP / 1.1"); cout<< request<< " \\\英寸; //这输出GET /path/test.htm HTTP / 1.1愉快地 //发送数据包(我知道没有错误检查,这只是一个例子) int numbytes; numbytes = send(socknum,request,sizeof(request),0)); cout<<"试图发送"<< sizeof(request )<<" bytes,sent"<< numbytes<<" bytes.\\\" ;; //此行输出5个字节已尝试,5个字节已发送,这是我的问题 这个问题是当我检查发送的数据包(带有数据包嗅探器)时,数据包只包含GET信息。 (它应该包含 " GET /path/test.htm HTTP / 1.1")。在我看来,只有变量 声明正确存储数据,并且strcat命令只是 ''链接''那些数据......'(我'可能是错的,但那就是我(模糊)对 情况的看法,无论如何) 如何解决这个问题?我知道如果我声明一个固定长度的请求, ie。 char request [1024] =" GET"; strcat (请求,路径); strcat(请求," HTTP / 1.1"); 输出所有内容,但请求用\填充0更混淆了 其他服务器。此外,如果路径比那个长,它会...... b 搞砸了...... 感谢您提前提供任何帮助, Patrick 解决方案 使用std :: string,因为否则你可能会使用错误的运算符 和函数,就像你在这里一样。 我试过用过一个std :: string,结果相同。 -Patrick < snip> 我尝试过使用std :: string,结果相同。 -Patrick 你不能使用char变量[],就像它会神奇地增长并分配 内存一样。事实上,你说,除了道路比这更长的时候,它还要搞砸了意味着你需要一个更好的数据结构, 一个分配并释放它自己的内存。如前所述, std :: string非常适合这项工作。如果你真的试一试并得到了 相同的结果我认为你不能正确使用该字符串。 使用std ::发布代码字符串,也许我们可以帮助您找到 问题所在的位置。除了使用std :: string之外,你必须制作一个足够大的 静态数组来处理任何情况。最后处理空字符 可以通过不发送该字节来完成。我不知道你是否使用 tcp或udp,而且两者都不是这个新闻组的主题,但如果你使用tcp而不是考虑投入一个循环你的发送功能 从缓冲区获取,检查和发送每个字节并退出 NULL字符而不是发送它。 , 克里斯托弗 Hi,I have the following code:char request[] = "GET ";strcat(request, path); //Path is the path section of a url ie. "/path/test.htm"strcat(request, " HTTP/1.1");cout<<request<<"\n"; //This outputs "GET /path/test.htm HTTP/1.1" happily//Send packet (I know there''s no error checking, this is just an example)int numbytes;numbytes = send(socknum, request, sizeof(request), 0));cout<<"Tried to send "<<sizeof(request)<<" bytes, sent "<<numbytes<<" bytes.\n";//This line outputs 5 bytes Tried, 5 bytes sent, which is my problemThe problem with this is that when I check the packet sent (with a packetsniffer) the packets only contains "GET " (It should contain"GET /path/test.htm HTTP/1.1"). It seems to me that only the variabledeclaration is storing the data properly, and the strcat command just''links'' that data somehow...(I''m probably wrong, but thats my (vague) idea of thesituation, anyway)How can I get around this? I know if I declare request of a fixed length,ie.char request[1024] = "GET ";strcat(request, path);strcat(request, " HTTP/1.1");It outputs everything, but request is padded with \0 which confuses theother server even more. Besides, if the path is longer than that, it''llscrew up...Thanks for any help in advance,Patrick 解决方案Use std::string, because otherwise you''re likely to use the wrong operatorsand functions, as you do here.I have tried using an std::string, with the same results.-Patrick <snip> I have tried using an std::string, with the same results. -PatrickYou can''t use char variable[] like it will magically grow and allocatememory by itself. The fact that you said, "besides if the path is longerthan that, it''ll screw up" implies that you need a better data structure,one that allocates and releases it''s memory by itself. As was said,std::string is perfect for the job. If you really gave it a try and got thesame results I don''t think you could have been using the string properly.Post the code using std::string and maybe we can help you find where theproblem is with it. Other than using std::string you will have to make astatic array big enough to handle any case. Dealing with the null characterat the end could be done by not sending that byte. I don''t know if yer usingtcp or udp, and both are really not on topic for this newsgroup, but if youare using tcp than consider putting a loop around your send functiongetting,checking, and sending each byte from the buffer and exiting on aNULL character instead of sending it.,Christopher 这篇关于strcat()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!