Save strings and integer in text file
-
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();
}
-
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();
}
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
-
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
-
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();
}
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