0

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");
        }
    }
Omnifarious
  • 54,333
  • 19
  • 131
  • 194
Prinks
  • 33
  • 3
  • 2
    Hardcoding 200 is very much the wrong thing to do there. You should do this: `write(newsockfd, final.data(), final.size());`. Drop `fnlPrice` as a variable completely. And the hardcoded `100` in the 'not found' case is also definitely the wrong thing to do. – Omnifarious Jul 20 '19 at 22:08
  • 1
    This doesn't address the question, but `if(found = true)` assigns the value `true` to `found`, then checked whether the result is true. I assume that's a typo for `if(found == true)`, but that's too longwinded. Just write `if(found)`. – Pete Becker Jul 20 '19 at 22:20
  • @Omnifarious and @ Prinks maybe I don't understand the code right, but it seems like you are writing to `.data()`.. This is undefined behaviour! (See https://en.cppreference.com/w/cpp/string/basic_string/data ) To do it right, you have to create a `char*` array, write to it, and use it in the constructor of a new `srd::string` (`std::string{pointer to char *}`) – jan.sende Jul 20 '19 at 23:51
  • 1
    @jan.sende `write` writes the string to somewhere else, not the other way around. – L. F. Jul 21 '19 at 00:48
  • Show us please how you validate on the client that you are receiving wrong data. Except the problems others mentioned maybe you print only part before new line '\n' character you are sending? – MBI Jul 21 '19 at 10:47
  • Ah I see now! But I don't think we can spot the bug in your code right now. Could you please post the receiving code as well. Also, instead of manual pointer conversion with `fnlPrice`, you could just use `static_cast(final.c_str())` or do a implicit conversion instead. – jan.sende Jul 21 '19 at 16:08

1 Answers1

0

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.

A M
  • 14,694
  • 5
  • 19
  • 44