0

I have a string like this: DialogTitle = IDD_SETTING_DLG in a save file (i have already stored it in an array called m_TextArray).

Now i want to get the "IDD_SETTING_DLG" part(or at least " IDD_SETTING_DLG") and store it in a CString variable. I used the Tokenize method but it didn't work.

Here are my codes:

BOOL CTab1::OnInitDialog()
{
    UpdateData();
    ReadSaveFile();
    SetTabDescription();
    UpdateData(FALSE);
    return TRUE;
}

void CTab1::DoDataExchange(CDataExchange* pDX)
{
    CDialog::DoDataExchange(pDX);
    DDX_Text(pDX, IDC_SHOWDES, m_ShowDes);
}

void CTab1::ReadSaveFile()
{
    if (!SaveFile.Open(SFLocation, CFile::modeRead | CFile::shareDenyWrite, &ex))
    {
        ReadSettingFile();
    }
    else
    {
        for (int i = 0; i < 100; i++)
        {
            SaveFile.ReadString(ReadLine);
            m_TextArray[i] = ReadLine.GetString();
        }
    }
}
void CTab1::SetTabDescription() //m_TextArray[2] is where i stored the text
{
    Position = 0;
    Seperator = _T("=");

    m_ShowDes = m_TextArray[2].Tokenize(Seperator, Position);

    while (!m_ShowDes.IsEmpty())
    {
                // get the next token
        m_ShowDes = m_TextArray[2].Tokenize(Seperator, Position);
    }
}

Anyone solution or hint would be very appreciated.

Turtle Lover
  • 45
  • 1
  • 10
  • 1
    In what way didn't it work? Please explain. – Ian Jun 04 '19 at 09:39
  • @Ian it didnt show anything on the ```IDC_SHOWDES``` Editbox – Turtle Lover Jun 04 '19 at 09:39
  • Try `strtok()`. Look into [this](https://stackoverflow.com/a/3147960/3197387) answer: – Sisir Jun 04 '19 at 09:40
  • @Ian you mean to call ```UpdateData()``` after the while loop is finished, or call it in the loop ? – Turtle Lover Jun 04 '19 at 09:49
  • At some after it, yes, but I realise you are calling it in the top function so I deleted my comment. – Ian Jun 04 '19 at 09:50
  • @Sisir when i replace the ```TCHAR * str = (LPCTSTR)cstr;``` with ```TCHAR * str = (LPCTSTR)m_TextArray[2];```, it said Error: ```a value of type LPCTSTR can not be used to initialize an entity of type TCHAR``` – Turtle Lover Jun 04 '19 at 09:51
  • Have you tried stepping through to see where it's going wrong? – Ian Jun 04 '19 at 09:53
  • @Ian ah yes, i just debugged it, and found out that the ```while loop``` looped 3 times: first time the ```m_ShowDes``` will hold the text ```DialogTitle```, second time it hold the ```IDD_SETTING_DLG``` that i want, and idk why it looped again and the value of ```m_ShowDes``` is changed back to ```""```. That's the reason nothing shown on the Edit Box – Turtle Lover Jun 04 '19 at 10:03
  • @Ian If you are getting a typecast error, try using this code `TCHAR * str = cstr.GetBuffer();` in place of `TCHAR * str = (LPCTSTR)cstr` – Sisir Jun 04 '19 at 10:07
  • Your while-loop is set to loop _while `m_ShowDes` is not empty_ ... that's why it only exits when it is empty. – Ian Jun 04 '19 at 10:08
  • @Ian If i input this line in the loop ```SetDlgItemText(IDC_SHOWDES,m_ShowDes)```-> it worked. But i don't think i this line is needed, right ? Because i've already do ```DDX_Text(pDX, IDC_SHOWDES, m_ShowDes)``` in DataExchange. – Turtle Lover Jun 04 '19 at 10:12
  • @Sisir now the error is ```This function or variable maybe unsafe``` – Turtle Lover Jun 04 '19 at 10:17
  • @TurtleLover The error message only has the answer to your problem. Use [strtok_s()](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/strtok-s-strtok-s-l-wcstok-s-wcstok-s-l-mbstok-s-mbstok-s-l?view=vs-2019) instead of strtok(). – Sisir Jun 04 '19 at 11:46
  • 1
    MFC prefers CString::Tokenize over C functions `strtok/strtok_s`, which are not even compatible with UTF16 – Barmak Shemirani Jun 04 '19 at 12:30

1 Answers1

1

Since you're merely looking for the part of the string that occurs after a token, there's no need to use Tokenize. Just find the position of the token character (your "=") and get everything after that:

void CTab1::SetTabDescription() //m_TextArray[2] is where i stored the text
{
    CString separator = _T("=");
    CString source = m_TextArray[2];

    // Get position of token...
    int position = source.Find(separator);

    // If token is found...
    if (position > -1 && source.GetLength() > position)
        m_ShowDes = source.Mid(position + 1);  // extract everything after token
}
Ian
  • 1,221
  • 1
  • 18
  • 30