samedi 20 juillet 2019

c_str() is only reading half of my string, why? How can I fix this? Is it a byte issue?

I am writing a client program and server program. On the server program, in order to write the result back to the client, I have to convert the string to const char* to put it in a const void* variable to use the write() function. The string itself is outputting the correct result when I checked, but when I use the c_str() function on the string, it is only outputting up until the first variable in the string. I am providing some code for reference (not sure if this is making any sense).

I have already tried all sorts of different ways to adjust the string, but nothing has worked yet.

Here are how the variables have been declared:

string final;
const void * fnlPrice;
carTable* table = new carTable[fileLength];

Here is the struct for the table:

struct carTable { string mm; // make and model string hPrice; // high price string lPrice; // low price };

Here is a snipped of the code with the issue, starting with updating the string variable, final, with text as well as the resulting string variables:

final = "The high price for that car is $" + table[a].hPrice + "\nThe low 
price for that car is $" + table[a].lPrice;;

    if(found = true)
    {
        fnlPrice = final.c_str();
        n = write(newsockfd,fnlPrice, 200);
        if (n < 0) 
        {
            error("ERROR writing to socket");
        }
    }
    else
    {
        n = write(newsockfd, "That make and model is not in 
                    the database. \n", 100);
        if (n < 0) 
        {
            error("ERROR writing to socket");
        }
    }

Aucun commentaire:

Enregistrer un commentaire