Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Save the content from List Control in text file vc++

Save the content from List Control in text file vc++

Scheduled Pinned Locked Moved C / C++ / MFC
c++
11 Posts 3 Posters 2 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • L lolici

    Hello everyone, I have created an editable list control (report style) with numbers. How could I save all content of list in a text file, each edited "number" below the other. I tried something like this:

    void CDataDialog::OnOK()
    {
    CWnd* pwndCtrl = GetFocus();
    // get the control ID which is
    // presently having the focus
    int ctrl_ID = pwndCtrl->GetDlgCtrlID();
    CString str;
    ofstream outFile;
    outFile.open("sample.txt");
    switch (ctrl_ID)
    { //if the control is the EditBox
    case IDC_EDIT1:
    //get the text from the EditBox
    GetDlgItemText(IDC_EDIT1, str);
    for (int i = 1; i <= m_nCon; i++)
    {
    outFile << str <

    but by this way I can save only the last edited item in text file. Thank you in advance!!

    D Offline
    D Offline
    David Crow
    wrote on last edited by
    #2

    Something like:

    CStdioFile file("sample.txt", CFile::modeCreate);

    int nCount = listControl.GetItemCount();
    for (int x = 0; x < nCount; x++)
    {
    CString strText = listControl.GetItemText(x, 0); // first column
    file.WriteString(strText);
    }

    file.Close();

    "One man's wage rise is another man's price increase." - Harold Wilson

    "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

    "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

    L 1 Reply Last reply
    0
    • D David Crow

      Something like:

      CStdioFile file("sample.txt", CFile::modeCreate);

      int nCount = listControl.GetItemCount();
      for (int x = 0; x < nCount; x++)
      {
      CString strText = listControl.GetItemText(x, 0); // first column
      file.WriteString(strText);
      }

      file.Close();

      "One man's wage rise is another man's price increase." - Harold Wilson

      "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

      "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

      L Offline
      L Offline
      lolici
      wrote on last edited by
      #3

      I wrote this:

      void CDataDialog::OnBnClickedOk()
      {
      // TODO: Add your control notification handler code here
      //CDialog::EndDialog(0);
      CStdioFile file("over1.txt", CFile::modeCreate);

      int nCount = m\_List.GetItemCount();
      for (int x = 0; x < nCount; x++)
      {
      	CString strText = m\_List.GetItemText(x, 0); // first column
      	
      	file.WriteString(strText);
      }
      
      file.Close();
      

      }

      and when execute it I get this statement: "Debug assertion failed". Is this because I already use CString CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const??? Sorry I didn't mention it in the first post.

      CString CDataDialog::GetItemText(
      HWND hWnd, int nItem, int nSubItem) const
      {
      LVITEM lvi;
      memset(&lvi, 0, sizeof(LVITEM));
      lvi.iSubItem = nSubItem;
      CString str;
      int nLen = 128;
      int nRes;
      do
      {
      nLen *= 2;
      lvi.cchTextMax = nLen;
      lvi.pszText = str.GetBufferSetLength(nLen);
      nRes = (int)::SendMessage(hWnd,
      LVM_GETITEMTEXT, (WPARAM)nItem,
      (LPARAM)&lvi);
      } while (nRes == nLen - 1);
      str.ReleaseBuffer();
      return str;
      }

      D V 2 Replies Last reply
      0
      • L lolici

        I wrote this:

        void CDataDialog::OnBnClickedOk()
        {
        // TODO: Add your control notification handler code here
        //CDialog::EndDialog(0);
        CStdioFile file("over1.txt", CFile::modeCreate);

        int nCount = m\_List.GetItemCount();
        for (int x = 0; x < nCount; x++)
        {
        	CString strText = m\_List.GetItemText(x, 0); // first column
        	
        	file.WriteString(strText);
        }
        
        file.Close();
        

        }

        and when execute it I get this statement: "Debug assertion failed". Is this because I already use CString CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const??? Sorry I didn't mention it in the first post.

        CString CDataDialog::GetItemText(
        HWND hWnd, int nItem, int nSubItem) const
        {
        LVITEM lvi;
        memset(&lvi, 0, sizeof(LVITEM));
        lvi.iSubItem = nSubItem;
        CString str;
        int nLen = 128;
        int nRes;
        do
        {
        nLen *= 2;
        lvi.cchTextMax = nLen;
        lvi.pszText = str.GetBufferSetLength(nLen);
        nRes = (int)::SendMessage(hWnd,
        LVM_GETITEMTEXT, (WPARAM)nItem,
        (LPARAM)&lvi);
        } while (nRes == nLen - 1);
        str.ReleaseBuffer();
        return str;
        }

        D Offline
        D Offline
        David Crow
        wrote on last edited by
        #4

        lolici wrote:

        ...and when execute it I get this statement: "Debug assertion failed".

        You'll need to step through the code using the debugger to find the line that is asserting.

        "One man's wage rise is another man's price increase." - Harold Wilson

        "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

        "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

        L 1 Reply Last reply
        0
        • D David Crow

          lolici wrote:

          ...and when execute it I get this statement: "Debug assertion failed".

          You'll need to step through the code using the debugger to find the line that is asserting.

          "One man's wage rise is another man's price increase." - Harold Wilson

          "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

          "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

          L Offline
          L Offline
          lolici
          wrote on last edited by
          #5

          The problem is here:

          int nCount = m_List.GetItemCount();

          I need another function than GetItemCount? :)

          D 1 Reply Last reply
          0
          • L lolici

            The problem is here:

            int nCount = m_List.GetItemCount();

            I need another function than GetItemCount? :)

            D Offline
            D Offline
            David Crow
            wrote on last edited by
            #6

            That line is NOT asserting. You need to step into the GetItemCount() method (until you find an ASSERT() macro).

            "One man's wage rise is another man's price increase." - Harold Wilson

            "Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons

            "You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles

            1 Reply Last reply
            0
            • L lolici

              I wrote this:

              void CDataDialog::OnBnClickedOk()
              {
              // TODO: Add your control notification handler code here
              //CDialog::EndDialog(0);
              CStdioFile file("over1.txt", CFile::modeCreate);

              int nCount = m\_List.GetItemCount();
              for (int x = 0; x < nCount; x++)
              {
              	CString strText = m\_List.GetItemText(x, 0); // first column
              	
              	file.WriteString(strText);
              }
              
              file.Close();
              

              }

              and when execute it I get this statement: "Debug assertion failed". Is this because I already use CString CDataDialog::GetItemText(HWND hWnd, int nItem, int nSubItem) const??? Sorry I didn't mention it in the first post.

              CString CDataDialog::GetItemText(
              HWND hWnd, int nItem, int nSubItem) const
              {
              LVITEM lvi;
              memset(&lvi, 0, sizeof(LVITEM));
              lvi.iSubItem = nSubItem;
              CString str;
              int nLen = 128;
              int nRes;
              do
              {
              nLen *= 2;
              lvi.cchTextMax = nLen;
              lvi.pszText = str.GetBufferSetLength(nLen);
              nRes = (int)::SendMessage(hWnd,
              LVM_GETITEMTEXT, (WPARAM)nItem,
              (LPARAM)&lvi);
              } while (nRes == nLen - 1);
              str.ReleaseBuffer();
              return str;
              }

              V Offline
              V Offline
              Victor Nijegorodov
              wrote on last edited by
              #7

              lolici wrote:

              I wrote this:

              void CDataDialog::OnBnClickedOk()
              {
              // TODO: Add your control notification handler code here
              //CDialog::EndDialog(0);
              CStdioFile file("over1.txt", CFile::modeCreate);

              int nCount = m_List.GetItemCount();

              What is m_List? Where and how is it defiled/initialized?

              lolici wrote:

              CString CDataDialog::GetItemText( HWND hWnd, int nItem, int nSubItem) const { LVITEM lvi; memset(&lvi, 0, sizeof(LVITEM)); lvi.iSubItem = nSubItem; CString str; int nLen = 128; int nRes; do { nLen *= 2; lvi.cchTextMax = nLen; lvi.pszText = str.GetBufferSetLength(nLen); nRes = (int)::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)nItem, (LPARAM)&lvi); } while (nRes == nLen - 1); str.ReleaseBuffer(); return str; }

              Why do you iplement your own function rather than use [CListCtrl::GetItemText (MFC)](https://msdn.microsoft.com/en-us/library/cbtzx5b1(v=vs.80).aspx) method?

              L 1 Reply Last reply
              0
              • V Victor Nijegorodov

                lolici wrote:

                I wrote this:

                void CDataDialog::OnBnClickedOk()
                {
                // TODO: Add your control notification handler code here
                //CDialog::EndDialog(0);
                CStdioFile file("over1.txt", CFile::modeCreate);

                int nCount = m_List.GetItemCount();

                What is m_List? Where and how is it defiled/initialized?

                lolici wrote:

                CString CDataDialog::GetItemText( HWND hWnd, int nItem, int nSubItem) const { LVITEM lvi; memset(&lvi, 0, sizeof(LVITEM)); lvi.iSubItem = nSubItem; CString str; int nLen = 128; int nRes; do { nLen *= 2; lvi.cchTextMax = nLen; lvi.pszText = str.GetBufferSetLength(nLen); nRes = (int)::SendMessage(hWnd, LVM_GETITEMTEXT, (WPARAM)nItem, (LPARAM)&lvi); } while (nRes == nLen - 1); str.ReleaseBuffer(); return str; }

                Why do you iplement your own function rather than use [CListCtrl::GetItemText (MFC)](https://msdn.microsoft.com/en-us/library/cbtzx5b1(v=vs.80).aspx) method?

                L Offline
                L Offline
                lolici
                wrote on last edited by
                #8

                m_list is the control variable I declared about list control. :) I used CListCtrl::GetItemText (MFC) but didn't work either :/

                V 1 Reply Last reply
                0
                • L lolici

                  m_list is the control variable I declared about list control. :) I used CListCtrl::GetItemText (MFC) but didn't work either :/

                  V Offline
                  V Offline
                  Victor Nijegorodov
                  wrote on last edited by
                  #9

                  Then please show your code how this variable was attached to the list control: did you use MFC DDX_... mechanism or something else?

                  L 1 Reply Last reply
                  0
                  • V Victor Nijegorodov

                    Then please show your code how this variable was attached to the list control: did you use MFC DDX_... mechanism or something else?

                    L Offline
                    L Offline
                    lolici
                    wrote on last edited by
                    #10

                    Here it is :) :

                    public: CListCtrl m_List;

                    void CDataDialog::DoDataExchange(CDataExchange* pDX)
                    {
                    CDialog::DoDataExchange(pDX);

                    DDX\_Control(pDX, IDC\_LIST1, m\_List);
                    

                    }

                    I could also upload the project or the executable if it is possible.

                    V 1 Reply Last reply
                    0
                    • L lolici

                      Here it is :) :

                      public: CListCtrl m_List;

                      void CDataDialog::DoDataExchange(CDataExchange* pDX)
                      {
                      CDialog::DoDataExchange(pDX);

                      DDX\_Control(pDX, IDC\_LIST1, m\_List);
                      

                      }

                      I could also upload the project or the executable if it is possible.

                      V Offline
                      V Offline
                      Victor Nijegorodov
                      wrote on last edited by
                      #11

                      lolici wrote:

                      I could also upload the project or the executable if it is possible.

                      Please, read the rules about asking the question here to know whether it is possible.

                      1 Reply Last reply
                      0
                      Reply
                      • Reply as topic
                      Log in to reply
                      • Oldest to Newest
                      • Newest to Oldest
                      • Most Votes


                      • Login

                      • Don't have an account? Register

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • World
                      • Users
                      • Groups