字符串转换问题(conver char * to string)
本帖最后由 wbx_2011 于 2012-12-06 16:00:09 编辑 我有一段代码是用来登录ORACLE数据库, 如果我密码直接用字符串的话登录成功,但如果我把它保存在char *的字符串时,登录就会失败,请高手看看怎么回事
函数原型
Connection * createConnection(
const string &userName,
const string &password,
const string &connectString="")=0;
char * dbpwd;
..... get db password from config file
env=Environment::createEnvironment(Environment::OBJECT);
cout<<"the db password is :"<<dbpwd<<endl;
con= env->createConnection(user_name,string(dbpwd),db_sid);
if(con){
return con;
}
else{
cout<<"failure createConnection!"<<endl;
return NULL;
}
the db password is :y1yBewwcbUS7d+rgSFC2NTzlT4Gv
terminate called after throwing an instance of 'oracle::occi::SQLException'
what(): ORA-01017: invalid username/password; logon denied
Aborted (core dumped)
con= env->createConnection(user_name,“y1yBewwcbUS7d+rgSFC2NTzlT4Gv”,db_sid);
pclose(pp);
int len=strlen(buf);
if(len == 0){
exit(1);
}
/* 加上这两句 */
if (buf[len - 1] == '\n'
[其他解释]
跟踪到createConnection函数里面,看看密码串是否被正确传入。
[其他解释]
createConnection是OCCI提供的函数,没有source code
[其他解释]
那么函数的原型是怎样的?dbpwd的获取过程是怎样的?
[其他解释]
char * dbpwd;
是怎么取密码的?
试试改为char数组
char dbpwd[256];
[其他解释]
class DB_Connection {
public:
DB_Connection();
DB_Connection(const DB_Connection& orig);
virtual ~DB_Connection();
Connection * GetDBConnection();
ResultSet * ExecuteSQL(string sql);
char * dbpwd;
private:
Environment *env;
Connection *con;
void GetDBPassword();
};
void DB_Connection::GetDBPassword(){
char buf[128];
FILE *pp;
memset(buf,0,sizeof(buf));
if ((pp = popen("/opt/getdbpswd", "r")) == NULL) {
cout<<"popen() error!"<<endl;
exit(1);
}
while (fgets(buf, 1000, pp)) {
cout<<"the db password is :"<<buf<<endl;
}
pclose(pp);
int len=strlen(buf);
if(len == 0){
exit(1);
}
dbpwd = (char*)malloc(sizeof(char)*(len+1));
if(dbpwd == NULL){
exit(1);
}
strcpy(dbpwd,buf);
}
[其他解释]
Connection * DB_Connection::GetDBConnection(){
env=Environment::createEnvironment(Environment::OBJECT);
cout<<"the db password is :"<<dbpwd<<endl;
con= env->createConnection(user_name,string(dbpwd),db_sid);
if(con){
return con;
}
else{
cout<<"failure createConnection!"<<endl;
return NULL;
}
}
[其他解释]
初学着,代码很乱,见谅
[其他解释]
29,数量多了一个,是不是多了"\0"
cout<<"the length of dbpwd is:"<<strlen(dbpwd)<<endl;
......
the db password is :y1yBewwcbUS7d+rgSFC2NTzlT4Gv
the length of dbpwd is:29
terminate called after throwing an instance of 'oracle::occi::SQLException'
what(): ORA-01017: invalid username/password; logon denied
[其他解释]
(buf[len - 1] == '\r')
buf[len - 1] = '\0';
cout<<"the length of dbpwd is:"<<strlen(dbpwd)<<endl;
string s(dbpwd,28);
con= env->createConnection(user_name,s,db_sid);