0

I am getting an error in the below program:

error: cannot convert 'std::string' to 'char*' for argument '1' to 'char*
strcpy(char*, const char*)'
std::string machine, account, passwd, dummyString; 
short sRc = FtpDestGet( BAC_REQ_DEST, machine, account, passwd, dummyString ); 
if ( sRc ) 
{ 
    fprintf( stderr, "Cannot retrieve target machine information from database, error code %ld\n", sRc ); 
    return ERROR; 
} 
strcpy( passwd, getenv(BAC_REQ_DEST));
1
  • When in C++ program like C++ans do and consider replacing the fprintf with insertions to std::cerr. Commented Jun 23, 2020 at 18:39

2 Answers 2

4

You don't need strcpy for C++ strings.

Do this: passwd = getenv(BAC_REQ_DEST);

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

Comments

3

strcpy() takes a char* pointer to a destination buffer to write to, and a const char* pointer to a source buffer to read from. But you are passing in a std::string object where the char* target buffer is expected. std::string does not implicitly convert to char*, hence the error.

You don't need strcpy() to assign a (const) char* to a std::string. std::string has an operator= for that purpose, eg:

passwd = getenv(BAC_REQ_DEST);

Also, you should be using std::cerr instead of fprintf(stderr):

std::cerr << "Cannot retrieve target machine information from database, error code " << sRc << "\n"; 

You are working in C++, not in C, so use C++ idioms.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.