0

I use the set_effect_block in the following code to convert a string to a fixed size string of 20 bytes.

class editoritems{

  public:

    editoritems(string= "");

    void set_effect_block(string paramnamestring)          //set effect block
    { 
      const char *effectnamevalue=paramnamestring.data();  
      int length=strlen(effectnamevalue);
      length=(length<20?length:19);
      strncpy_s(effe_block,effectnamevalue,length);
      effe_block[length]='\0';
    }

    string get_effect_block()const{return effe_block;}

  private:

    char effe_block[20];
};

editoritems::editoritems(string h)
{
  set_effect_block(h);
}

Is this a good way to do that ? Is there any faster way ?

1
  • Well you certainly don't need strlen to get the length of a std::string. Commented May 5, 2013 at 8:05

1 Answer 1

3

Try this:

void set_effect_block(string paramnamestring)
{
    size_t copied = paramnamestring.copy(effe_block, 19);
    effe_block[copied] = '\0';
}

BTW: You might want to consider using const std::string& paramnamestring as parameter for editoritems::set_effect_block(), so that the string does not need to be copied to be passed into the function.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.