Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Sign In with Facebook Sign In with Google Sign In with OpenID

Categories

We have migrated to a new platform! Please note that you will need to reset your password to log in (your credentials are still in-tact though). Please contact lee@programmersheaven.com if you have questions.
Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

CString to char[]

outlawhackeroutlawhacker Posts: 29Member
Hello

I'm trying to make an old C console program work in Visual C++. It's a program that uses file processing to a binary file.

I use this struct:

struct sample
{
char name[41];
char phone[21];
}Sample;

When I use the gets() function in my console program I can take whatever the user input to a char buffer[]. With this I can save any type of character in my database.

But in VC++ 6, I'm using an edit box with CString. If I take the contents of the edit box and use strcpy() to copy the CString to the
char array in a Sample struct things go wrong. The program doesn't complain but something goes wrong in adding this struct to the data file. When added the word can't be found.

It's alright as long as I don't use digits and odd characters.

How can I get the text in the editbox without any changes to a char array? If the user inputs Name as: bill12_e()/&%_- then I would like that exact name to be in the data file.

thanks

Comments

  • stoberstober Posts: 9,765Member ✭✭✭

    leave the structure the way you had it and just copy the CString text into the desired element.

    Also, make sure you call UpdateData().

    [code]
    void CTestDlg::OnOK()
    {
    char name[255];
    // move data fron control to m_text
    UpdateData(TRUE);
    // copy to name
    strcpy(name,(LPCTSTR)m_text);
    CDialog::OnOK();
    }
    [/code]


  • outlawhackeroutlawhacker Posts: 29Member
    seems to work! wow... a single (LPCTSTR) made so much difference...

    what exactly is (LPCTSTR)??

    and thanks a lot for the fast and accurate answer, dude! :)


    : leave the structure the way you had it and just copy the CString text into the desired element.
    :
    : Also, make sure you call UpdateData().
    :
    : [code]
    : void CTestDlg::OnOK()
    : {
    : char name[255];
    : // move data fron control to m_text
    : UpdateData(TRUE);
    : // copy to name
    : strcpy(name,(LPCTSTR)m_text);
    : CDialog::OnOK();
    : }
    : [/code]
    :
    :
    :

  • drzarkovdrzarkov Posts: 57Member
    just a little remark: as soon you compile with UNICODE...then this wont work anymore. Its better to use TCHAR instead of char and use the conversion macros _T (for defining const-strings), CA2T (for converting char into TCHAR) and CT2A (for converting TCHAR to char).

    LPCTSTR is const string out of TCHARs. LPTSTR is the non-constant version. If UNICODE is defined then TCHAR is 2 bytes-wide.

    Greetings
    E

    : seems to work! wow... a single (LPCTSTR) made so much difference...
    :
    : what exactly is (LPCTSTR)??
    :
    : and thanks a lot for the fast and accurate answer, dude! :)
    :
    :
    : : leave the structure the way you had it and just copy the CString text into the desired element.
    : :
    : : Also, make sure you call UpdateData().
    : :
    : : [code]
    : : void CTestDlg::OnOK()
    : : {
    : : char name[255];
    : : // move data fron control to m_text
    : : UpdateData(TRUE);
    : : // copy to name
    : : strcpy(name,(LPCTSTR)m_text);
    : : CDialog::OnOK();
    : : }
    : : [/code]
    : :
    : :
    : :
    :
    :

  • outlawhackeroutlawhacker Posts: 29Member
    OK thanks.

    But, in what situation do I need to "compile with UNICODE"? If I'm using a simple program with file processing and some windows GUI, will this be crucial? :O

    if not thanks for the remark, though. I'll keep it in mind for further use. ;)


    : just a little remark: as soon you compile with UNICODE...then this wont work anymore. Its better to use TCHAR instead of char and use the conversion macros _T (for defining const-strings), CA2T (for converting char into TCHAR) and CT2A (for converting TCHAR to char).
    :
    : LPCTSTR is const string out of TCHARs. LPTSTR is the non-constant version. If UNICODE is defined then TCHAR is 2 bytes-wide.
    :
    : Greetings
    : E
    :
  • stoberstober Posts: 9,765Member ✭✭✭
    compile for UNICODE (1) non-English languages, or (2) WinCE programs using eVC++ 3.0 or eVC++ 4.0 compilers.
  • J.B.J.B. Posts: 45Member
    :
    : leave the structure the way you had it and just copy the CString text into the desired element.
    :
    : Also, make sure you call UpdateData().
    :
    : [code]
    : void CTestDlg::OnOK()
    : {
    : char name[255];
    : // move data fron control to m_text
    : UpdateData(TRUE);
    : // copy to name
    : strcpy(name,(LPCTSTR)m_text);
    : CDialog::OnOK();
    : }
    : [/code]

    Hi stober,
    may I ask what UpdateData acutally does here. In the MSDN, its function is to "Call this member function to initialize data in a dialog box, or to retrieve and validate dialog data.". In this particular example, why would UpdataData be necessary here. Also, I'd think the m_text here is a 'value' type CString variable member to say for Edit Box for example?

    Thanks

  • stoberstober Posts: 9,765Member ✭✭✭
    : :
    : : leave the structure the way you had it and just copy the CString text into the desired element.
    : :
    : : Also, make sure you call UpdateData().
    : :
    : : [code]
    : : void CTestDlg::OnOK()
    : : {
    : : char name[255];
    : : // move data fron control to m_text
    : : UpdateData(TRUE);
    : : // copy to name
    : : strcpy(name,(LPCTSTR)m_text);
    : : CDialog::OnOK();
    : : }
    : : [/code]
    :
    : Hi stober,
    : may I ask what UpdateData acutally does here. In the MSDN, its function is to "Call this member function to initialize data in a dialog box, or to retrieve and validate dialog data.". In this particular example, why would UpdataData be necessary here. Also, I'd think the m_text here is a 'value' type CString variable member to say for Edit Box for example?
    :
    : Thanks
    :
    :

    UpdateDate(TRUE) moves the text from the control to the program's local variable, in this case m_text. There are, like all other things in Windows programming, several other ways to accomplish the same thing without using UpdateData() or member variables attached to a control. For example one alternative is like this:
    [code]
    void CTestDlg::OnOK()
    {
    char name[255];
    // copy to name
    GetDlgItemText(IDC_EDIT1, name, sizeof(name));
    CDialog::OnOK();
    }


    [/code]


Sign In or Register to comment.