I know there's a lot information and threads about char array to string conversion, but I tried a lot of "solutions" without any success. I tried this solution which is the closest to my issue but didn't work either.
I have the following code:
recvlen = recvfrom(s, recvbuf, BUFLEN, 0, (struct sockaddr *) &si_other, &slen); if (recvlen < 0) { printf("socket() failed with error code : %d", WSAGetLastError()); } string msgrcv(recvbuf, recvlen); printf("String : %s \n", msgrcv); //Prints the weird characters printf("Array : %s \n", recvbuf); //Prints ok the result Any ideas what I'm missing or is wrong?
printfcan't handle the C++ string.printf("String : %s \n", msgrcv.c_str())as%sexpects a pointer to char array, not astd::stringobject.