Save the content from List Control in text file vc++
-
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!!
-
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!!
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
-
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
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;
} -
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;
}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
-
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
-
The problem is here:
int nCount = m_List.GetItemCount();
I need another function than GetItemCount? :)
That line is NOT asserting. You need to step into the
GetItemCount()
method (until you find anASSERT()
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
-
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;
}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?
-
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?
-
m_list is the control variable I declared about list control. :) I used CListCtrl::GetItemText (MFC) but didn't work either :/
Then please show your code how this variable was attached to the list control: did you use MFC DDX_... mechanism or something else?
-
Then please show your code how this variable was attached to the list control: did you use MFC DDX_... mechanism or something else?
-
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.
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.