简体   繁体   中英

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");
        }
    }

Unfortunately your code does not make any sense. And that may be your major problem. You should rewrite you code end eliminate the bugs.

  • Switch on all compiler warnings and eliminate the warnings.
  • Do not use new and pointers. Never
  • Do not use C-Style arrays. So, something with []. Never. Use STL containers
  • Always initialize all variables. Always. Even if you assign an other value in the next line
  • Do not use magic constants like 200 (The size of the string is final.size())
  • If an error happens then print the error text with strerror (or a compatible function)
  • Make sure that your array itself and the array values are initalized
  • To test your function, write to socket 1 (_write(1,fnlPrice,final.size()); 1 is equal to std::cout
  • There is no need to use the void pointer. You can use n = _write(newsockfd, final.c_str(), final.size()); directly

If you want a detailed answer here on SO then you need to post your compiled code. I have rewritten your function and tested it. It works for me and prints the complete string. So, there is a bug in an other part of your code that we cannot not see.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM