首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

字符串转换有关问题(conver char * to string)

2012-12-26 
字符串转换问题(conver char * to string)本帖最后由 wbx_2011 于 2012-12-06 16:00:09 编辑我有一段代码

字符串转换问题(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);


请问是哪儿出错了
[最优解释]
看起来没什么问题,你打印dbpwd的长度看看,是否文件中多了些什么空格/TAB之类的不以察觉的符号。
[其他解释]
用的是fgets(buf, 1000, pp)
怀疑密码之后有个换行符'\n'


       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';

[其他解释]
引用:
29,数量多了一个,是不是多了"\0"

C/C++ code?123456789 cout<<"the length of dbpwd is:"<<strlen(dbpwd)<<endl; ......the db password is :y1yBewwcbUS7d+rgSFC2NTzlT4Gv the length of dbpwd is:29 terminat……


明显是多了换行符。
密码后面有个空行啊!
[其他解释]
取28个就登录成功了,请解释一下好吗

 cout<<"the length of dbpwd is:"<<strlen(dbpwd)<<endl;
     string s(dbpwd,28);
     con= env->createConnection(user_name,s,db_sid);

[其他解释]
fgets会捎上换行符的。
[其他解释]
保险起见,获取后用trim类函数去掉多余的字符。

引用:
取28个就登录成功了,请解释一下好吗
C/C++ code?123 cout<<"the length of dbpwd is:"<<strlen(dbpwd)<<endl;     string s(dbpwd,28);     con= env->createConnection(user_name,s,db_sid);

[其他解释]
用debug看dbpwd的内容吧,怀疑最后一个字符是空格什么的。

热点排行