CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 20 of 20
  1. #16
    Join Date
    Feb 2005
    Posts
    2,160

    Re: converting stringc to char* ?

    Personally, I'd use sprintf() or, if using VC, CString::Format() to build the sql request, but I prefer C-style coding over std containers for the most part (that's the way I learned):

    Code:
    int itemnumber=101;
    
    char sqlbuf[100];  //make it big enough to hold your entire statement
    
    sprintf(buf,"SELECT * FROM USERTABLE WHERE itemnumber='%d' AND loginpw='%s' ORDER BY loginid",itemnumber,"MyPassword");
    if(!db.Execute(sqlbuf,tbl)){
      //...
    }

  2. #17
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: converting stringc to char* ?

    You should use one or the other approach. Mixing them up just tends to cause confusion. Right now, you're using a combination of strcpy() and std::string fairly intermingled, which I'd consider bad form. Also, don't over-use the c_str() method; it isn't needed for cout or for std::string concatenation, or for pretty much anything else except functions requiring a const char* explicitly.

  3. #18
    Join Date
    Apr 1999
    Posts
    27,449

    Re: converting stringc to char* ?

    Quote Originally Posted by digoxy View Post
    Hello Paul, I did drop this in just to see what it would do, I have since tokenized the login id and pw. Here is the way the I did this as a char[200].
    Do you have a check if you have exceeded 200 characters? If you don't then this is deemed a vulnerability in your program, as buffer overflow can occur.

    That's why std::string is safer.
    If you see anything here that might be of use to change it up a bit, I dont mind playing with it. I have it in the script just commented now so I can ding around with it!
    Just fix the compiler errors. If all the problem is just quotes, it takes a few seconds to fix them.

    As to char*, the following should be safer:
    Code:
    #include <string>
    #include <vector>
    //...
    std::string str = "select * from usertable where loginid='";
    str += logidin + "' and loginpw ='" + logidin + "' order by loginid";
    
    std::vector<char> tempV(str.begin(), str.end());
    tempV.push_back(0);
    
    if(!db.Execute(&tempV[0], tbl))
    { }
    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; March 18th, 2010 at 12:17 PM. Reason: Added push_back(0) to null-terminate the string

  4. #19
    Lindley is offline Elite Member Power Poster
    Join Date
    Oct 2007
    Location
    Seattle, WA
    Posts
    10,895

    Re: converting stringc to char* ?

    ^I'm not sure that doing that would properly null-terminate the string. You may need to push_back a 0 at the end. Actually, it would be good to know either way for certain.

  5. #20
    Join Date
    Apr 1999
    Posts
    27,449

    Re: converting stringc to char* ?

    Quote Originally Posted by Lindley View Post
    ^I'm not sure that doing that would properly null-terminate the string. You may need to push_back a 0 at the end. Actually, it would be good to know either way for certain.
    Yes, it wouldn't hurt to push_back a 0 onto that.

    Regards,

    Paul McKenzie

Page 2 of 2 FirstFirst 12

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured