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 strings and integer in text file

Save strings and integer in text file

Scheduled Pinned Locked Moved C / C++ / MFC
question
4 Posts 3 Posters 0 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 Offline
    L Offline
    lolici
    wrote on last edited by
    #1

    Hello everyone! How could I save the value of a member variable (int m_nCon) in the same text file where I have already save the content of a list control(declared as m_List)?? For the list control I used WriteString(). For the integer member variable I want to save, I used

    file.Write(&m_nCon, sizeof(int));

    but I got only a big blank space :/ Here is the code:

    void CDataDialog::OnBnClickedOk()
    {
    // TODO: Add your control notification handler code here
    CStdioFile file(L"sample.txt", CFile::modeCreate |
    CFile::modeWrite);

    int nCount = m\_List.GetItemCount();
    for (int x = 0; x < nCount; x++)
    {
    	CString strText1 = m\_List.GetItemText(x, 1); // first column
    	file.WriteString(strText1);
    	file.WriteString(\_T("\\n"));
    	CString strText2 = m\_List.GetItemText(x, 2); // second column
    	file.WriteString(strText2);
    	file.WriteString(\_T("\\n"));
    

    }

    file.Close();
    

    }

    D P 2 Replies Last reply
    0
    • L lolici

      Hello everyone! How could I save the value of a member variable (int m_nCon) in the same text file where I have already save the content of a list control(declared as m_List)?? For the list control I used WriteString(). For the integer member variable I want to save, I used

      file.Write(&m_nCon, sizeof(int));

      but I got only a big blank space :/ Here is the code:

      void CDataDialog::OnBnClickedOk()
      {
      // TODO: Add your control notification handler code here
      CStdioFile file(L"sample.txt", CFile::modeCreate |
      CFile::modeWrite);

      int nCount = m\_List.GetItemCount();
      for (int x = 0; x < nCount; x++)
      {
      	CString strText1 = m\_List.GetItemText(x, 1); // first column
      	file.WriteString(strText1);
      	file.WriteString(\_T("\\n"));
      	CString strText2 = m\_List.GetItemText(x, 2); // second column
      	file.WriteString(strText2);
      	file.WriteString(\_T("\\n"));
      

      }

      file.Close();
      

      }

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

      If you used Write() to write the integer and then used something like Notepad to view the file, it will indeed not look like you'd expect. This is a difference between "binary" and "text" files. Try something like:

      CString s;
      s.Format(_T("%d\n"), m_nCon);
      file.WriteString(s);

      "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

        If you used Write() to write the integer and then used something like Notepad to view the file, it will indeed not look like you'd expect. This is a difference between "binary" and "text" files. Try something like:

        CString s;
        s.Format(_T("%d\n"), m_nCon);
        file.WriteString(s);

        "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

        It worked!!I didn't know the difference! Thank you very much for your answer and the explication!! :)

        1 Reply Last reply
        0
        • L lolici

          Hello everyone! How could I save the value of a member variable (int m_nCon) in the same text file where I have already save the content of a list control(declared as m_List)?? For the list control I used WriteString(). For the integer member variable I want to save, I used

          file.Write(&m_nCon, sizeof(int));

          but I got only a big blank space :/ Here is the code:

          void CDataDialog::OnBnClickedOk()
          {
          // TODO: Add your control notification handler code here
          CStdioFile file(L"sample.txt", CFile::modeCreate |
          CFile::modeWrite);

          int nCount = m\_List.GetItemCount();
          for (int x = 0; x < nCount; x++)
          {
          	CString strText1 = m\_List.GetItemText(x, 1); // first column
          	file.WriteString(strText1);
          	file.WriteString(\_T("\\n"));
          	CString strText2 = m\_List.GetItemText(x, 2); // second column
          	file.WriteString(strText2);
          	file.WriteString(\_T("\\n"));
          

          }

          file.Close();
          

          }

          P Offline
          P Offline
          Premnath Mali
          wrote on last edited by
          #4

          To write list control records into text file use structure you can write and read entire row of list control. it will be better to seek the file pointer while reading and writing

          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