Save function not working

Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved

Join Date: Jun 2009
Posts: 104
Reputation: goody11 is an unknown quantity at this point 
Solved Threads: 1
goody11 goody11 is offline Offline
Junior Poster

Save function not working

 
0
  #1
Jul 5th, 2009
I was making a function to save info and it doesn't work. It's
actually made up of 2 functions. I've eliminated my problems down to
the second function and can't figure out what to do. Here is the
function:
  1. void DoFileSave(HWND hwnd)
  2. {
  3. OPENFILENAME ofn;
  4. char szFileName[MAX_PATH] = "";
  5. ZeroMemory(&ofn, sizeof(ofn));
  6. ofn.lStructSize = sizeof(OPENFILENAME);
  7. ofn.hwndOwner = hwnd;
  8. ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*
  9. \0";
  10. ofn.lpstrFile = szFileName;
  11. ofn.nMaxFile = MAX_PATH;
  12. ofn.lpstrDefExt = "txt";
  13. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY |
  14. OFN_OVERWRITEPROMPT;
  15. if(GetSaveFileName(&ofn))
  16. {
  17. int i, a;
  18. char etxt[100];
  19. i = SendMessage(IDC_LIST, LB_GETCOUNT, 0, 0); //gets the number of
  20. items in listbox
  21. for (a=0; a<i; a++)
  22. {
  23. SendMessage(IDC_LIST, LB_GETTEXT, a, etxt[a]); //get all text
  24. }
  25. HWND hEdit = GetDlgItem(hwnd, etxt[a]);
  26. SaveTextFileFromListBox(hEdit, szFileName);
  27. }
  28. }
Here are the errors I get:
error C2275: 'HWND' : illegal use of this type as an expression
error C2146: syntax error : missing ';' before identifier 'hEdit'
error C2065: 'hEdit' : undeclared identifier
error C2065: 'hEdit' : undeclared identifier
Last edited by Ancient Dragon; Jul 5th, 2009 at 3:19 pm. Reason: add code tags
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Save function not working

 
0
  #2
Jul 5th, 2009
Please use code tags and post the smallest fully compilable example that demonstrates the problem.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 104
Reputation: goody11 is an unknown quantity at this point 
Solved Threads: 1
goody11 goody11 is offline Offline
Junior Poster

Re: Save function not working

 
0
  #3
Jul 5th, 2009
Ok this is the smallest I could make.

Here is the header file:
  1. //{{NO_DEPENDENCIES}}
  2. // Microsoft Developer Studio generated include file.
  3. // Used by ctl_one.rc
  4. //
  5. #define IDD_MAIN 101
  6. #define IDC_TEXT 1000
  7. #define IDC_OTEXT 1001
  8. #define IDC_LIST 1002
  9. #define IDC_OLIST 1011
  10. #define IDC_ADD 1003
  11. #define IDC_SHOWCOUNT 1006
  12. #define IDC_SAVE 1008
  13.  
  14. // Next default values for new objects
  15. //
  16. #ifdef APSTUDIO_INVOKED
  17. #ifndef APSTUDIO_READONLY_SYMBOLS
  18. #define _APS_NEXT_RESOURCE_VALUE 102
  19. #define _APS_NEXT_COMMAND_VALUE 40001
  20. #define _APS_NEXT_CONTROL_VALUE 1007
  21. #define _APS_NEXT_SYMED_VALUE 101
  22. #endif
  23. #endif

Here is the resource file:
  1. //Microsoft Developer Studio generated resource script.
  2. //
  3. #include "resource.h"
  4.  
  5. #define APSTUDIO_READONLY_SYMBOLS
  6. /////////////////////////////////////////////////////////////////////////////
  7. //
  8. // Generated from the TEXTINCLUDE 2 resource.
  9. //
  10. #ifndef __BORLANDC__
  11. #include "winres.h"
  12. #endif
  13.  
  14. /////////////////////////////////////////////////////////////////////////////
  15. #undef APSTUDIO_READONLY_SYMBOLS
  16.  
  17. /////////////////////////////////////////////////////////////////////////////
  18. // English (U.S.) resources
  19.  
  20. #if !defined(AFX_RESOURCE_DLL) || defined(AFX_TARG_ENU)
  21. #ifdef _WIN32
  22. LANGUAGE LANG_ENGLISH, SUBLANG_ENGLISH_US
  23. #pragma code_page(1252)
  24. #endif //_WIN32
  25.  
  26. #ifdef APSTUDIO_INVOKED
  27. /////////////////////////////////////////////////////////////////////////////
  28. //
  29. // TEXTINCLUDE
  30. //
  31.  
  32. 1 TEXTINCLUDE DISCARDABLE
  33. BEGIN
  34. "resource.h\0"
  35. END
  36.  
  37. 2 TEXTINCLUDE DISCARDABLE
  38. BEGIN
  39. "#ifndef __BORLANDC__\r\n"
  40. "#include ""winres.h""\r\n"
  41. "#endif\r\n"
  42. "\0"
  43. END
  44.  
  45. 3 TEXTINCLUDE DISCARDABLE
  46. BEGIN
  47. "\0"
  48. END
  49.  
  50. #endif // APSTUDIO_INVOKED
  51.  
  52.  
  53. /////////////////////////////////////////////////////////////////////////////
  54. //
  55. // Dialog
  56. //
  57.  
  58. IDD_MAIN DIALOG DISCARDABLE 0, 0, 300, 302
  59. STYLE DS_MODALFRAME | DS_CENTER | WS_POPUP | WS_CAPTION | WS_SYSMENU
  60. CAPTION "Spanish Word Editor"
  61. FONT 8, "MS Sans Serif"
  62. BEGIN
  63. LTEXT "Add Spanish",IDC_STATIC,7,10,56,8
  64. EDITTEXT IDC_TEXT,50,7,60,14,ES_AUTOHSCROLL
  65. LTEXT "Add English",IDC_STATIC,112,10,56,8
  66. EDITTEXT IDC_OTEXT,151,7,60,14,ES_AUTOHSCROLL
  67. LISTBOX IDC_LIST,7,25,103,106,LBS_NOINTEGRALHEIGHT |
  68. LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
  69. LISTBOX IDC_OLIST,112,25,103,106,LBS_NOINTEGRALHEIGHT |
  70. LBS_EXTENDEDSEL | WS_VSCROLL | WS_TABSTOP
  71. PUSHBUTTON "&Add",IDC_ADD,220,30,50,14
  72. PUSHBUTTON "&Save",IDC_SAVE,220,80,50,14
  73. END
  74.  
  75.  
  76. /////////////////////////////////////////////////////////////////////////////
  77. //
  78. // DESIGNINFO
  79. //
  80.  
  81. #ifdef APSTUDIO_INVOKED
  82. GUIDELINES DESIGNINFO DISCARDABLE
  83. BEGIN
  84. IDD_MAIN, DIALOG
  85. BEGIN
  86. LEFTMARGIN, 7
  87. RIGHTMARGIN, 200
  88. VERTGUIDE, 145
  89. VERTGUIDE, 150
  90. TOPMARGIN, 7
  91. BOTTOMMARGIN, 149
  92. END
  93. END
  94. #endif // APSTUDIO_INVOKED
  95.  
  96. #endif // English (U.S.) resources
  97. /////////////////////////////////////////////////////////////////////////////

And here's the CPP file:
  1. #include <windows.h>
  2. #include "resource.h"
  3.  
  4.  
  5. BOOL SaveTextFileFromListBox(HWND hEdit, LPCTSTR pszFileName)
  6. {
  7. HANDLE hFile;
  8. BOOL bSuccess = FALSE;
  9.  
  10. hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
  11. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  12. if(hFile != INVALID_HANDLE_VALUE)
  13. {
  14. DWORD dwTextLength;
  15.  
  16. dwTextLength = GetWindowTextLength(hEdit);
  17. // No need to bother if there's no text.
  18. if(dwTextLength > 0)
  19. {
  20. LPSTR pszText;
  21. DWORD dwBufferSize = dwTextLength + 1;
  22.  
  23. pszText = GlobalAlloc(GPTR, dwBufferSize);
  24. if(pszText != NULL)
  25. {
  26. if(GetWindowText(hEdit,pszText, dwBufferSize))
  27. {
  28. DWORD dwWritten;
  29.  
  30. if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
  31. bSuccess = TRUE;
  32. }
  33. GlobalFree(pszText);
  34. }
  35. }
  36. CloseHandle(hFile);
  37. }
  38. return bSuccess;
  39. }
  40.  
  41. void DoFileSave(HWND hwnd)
  42. {
  43.  
  44. OPENFILENAME ofn;
  45. char szFileName[MAX_PATH] = "";
  46.  
  47. ZeroMemory(&ofn, sizeof(ofn));
  48.  
  49. ofn.lStructSize = sizeof(OPENFILENAME);
  50. ofn.hwndOwner = hwnd;
  51. ofn.lpstrFilter = "Text Files (*.txt)\0*.txt\0All Files (*.*)\0*.*\0";
  52. ofn.lpstrFile = szFileName;
  53. ofn.nMaxFile = MAX_PATH;
  54. ofn.lpstrDefExt = "txt";
  55. ofn.Flags = OFN_EXPLORER | OFN_PATHMUSTEXIST | OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT;
  56.  
  57. if(GetSaveFileName(&ofn))
  58. {
  59. int i, a;
  60. char etxt[100];
  61. i = SendMessage(IDC_LIST, LB_GETCOUNT, 0, 0); //gets the number of items in listbox
  62. for (a=0; a<i; a++)
  63. {
  64. SendMessage(IDC_LIST, LB_GETTEXT, a, etxt[a]); //get all text
  65.  
  66. }
  67. HWND hEdit = GetDlgItem(hwnd, etxt[a]);
  68. SaveTextFileFromListBox(hEdit, szFileName);
  69. }
  70. }
  71.  
  72.  
  73.  
  74.  
  75.  
  76. BOOL CALLBACK DlgProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam)
  77. {
  78. switch(Message)
  79. {
  80.  
  81. case WM_COMMAND:
  82. switch(LOWORD(wParam))
  83. {
  84. case IDC_ADD:
  85. {
  86.  
  87. int nTimes = 1;
  88.  
  89. // Then we get the string they entered
  90. // First we need to find out how long it is so that we can
  91. // allocate some memory
  92. int len = GetWindowTextLength(GetDlgItem(hwnd, IDC_TEXT));
  93. int len2 = GetWindowTextLength(GetDlgItem(hwnd, IDC_OTEXT));
  94. if(len > 0 && len2 > 0)
  95. {
  96. // Now we allocate, and get the string into our buffer
  97.  
  98. int i;
  99. char* buf;
  100.  
  101. buf = (char*)GlobalAlloc(GPTR, len + 1);
  102. GetDlgItemText(hwnd, IDC_TEXT, buf, len + 1);
  103.  
  104. // Now we add the string to the list box however many times
  105. // the user asked us to.
  106.  
  107. for(i = 0;i < nTimes; i++)
  108. {
  109. int index = SendDlgItemMessage(hwnd, IDC_LIST, LB_ADDSTRING, 0, (LPARAM)buf);
  110. // Here we are associating the value nTimes with the item
  111. // just for the heck of it, we'll use it to display later.
  112. // Normally you would put some more useful data here, such
  113. // as a pointer.
  114. SendDlgItemMessage(hwnd, IDC_LIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
  115. }
  116.  
  117. // Dont' forget to free the memory!
  118. GlobalFree((HANDLE)buf);
  119.  
  120.  
  121.  
  122. buf = (char*)GlobalAlloc(GPTR, len2 + 1);
  123. GetDlgItemText(hwnd, IDC_OTEXT, buf, len2 + 1);
  124.  
  125. // Now we add the string to the list box however many times
  126. // the user asked us to.
  127.  
  128. for(i = 0;i < nTimes; i++)
  129. {
  130. int index = SendDlgItemMessage(hwnd, IDC_OLIST, LB_ADDSTRING, 0, (LPARAM)buf);
  131. // Here we are associating the value nTimes with the item
  132. // just for the heck of it, we'll use it to display later.
  133. // Normally you would put some more useful data here, such
  134. // as a pointer.
  135. SendDlgItemMessage(hwnd, IDC_OLIST, LB_SETITEMDATA, (WPARAM)index, (LPARAM)nTimes);
  136. }
  137.  
  138. // Dont' forget to free the memory!
  139. GlobalFree((HANDLE)buf);
  140. }
  141. else
  142. {
  143. MessageBox(hwnd, "You didn't enter anything!", "Warning", MB_OK);
  144. }
  145.  
  146. }
  147. break;
  148.  
  149. break;
  150. case IDC_SAVE:
  151. DoFileSave(hwnd);
  152. break;
  153. }
  154. break;
  155. case WM_CLOSE:
  156. EndDialog(hwnd, 0);
  157. break;
  158. default:
  159. return FALSE;
  160. }
  161. return TRUE;
  162. }
  163.  
  164. int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance,
  165. LPSTR lpCmdLine, int nCmdShow)
  166. {
  167. return DialogBox(hInstance, MAKEINTRESOURCE(IDD_MAIN), NULL, DlgProc);
  168. }
Sorry if it's still a little lengthy.
Reply With Quote Quick reply to this message  
Join Date: Feb 2008
Posts: 628
Reputation: daviddoria is a jewel in the rough daviddoria is a jewel in the rough daviddoria is a jewel in the rough 
Solved Threads: 46
daviddoria daviddoria is offline Offline
Practically a Master Poster

Re: Save function not working

 
0
  #4
Jul 5th, 2009
Unfortunately I don't use windows so I can't try to help, but I have a hard time believing that error can't be produced in < 20 lines...
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Save function not working

 
0
  #5
Jul 5th, 2009
I compiled the *.cpp file with VC++ 2008 Express and got some errors
1>c:\dvlp\wintest\wintest\wintest.cpp(23) : error C2440: '=' : cannot convert from 'HGLOBAL' to 'LPSTR'
1> Conversion from 'void*' to pointer to non-'void' requires an explicit cast
1>c:\dvlp\wintest\wintest\wintest.cpp(61) : error C2664: 'SendMessageA' : cannot convert parameter 1 from 'int' to 'HWND'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>c:\dvlp\wintest\wintest\wintest.cpp(64) : error C2664: 'SendMessageA' : cannot convert parameter 1 from 'int' to 'HWND'
1> Conversion from integral type to pointer type requires reinterpret_cast, C-style cast or function-style cast
1>Build log was saved at "file://c:\dvlp\wintest\wintest\Debug\BuildLog.htm"
1>wintest - 3 error(s), 0 warning(s)
As for the first error: GlobalAlloc() returns a HGLOBAL object, not a char*. And GlobalAlloc() is just an unnecessary complication, just use standard c++ dynamic memory allocation, such as new and delete[].

The first argument to SendMessage() must be a window handle HWIND, not a macro from the resource.h file.
Last edited by Ancient Dragon; Jul 5th, 2009 at 3:36 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 104
Reputation: goody11 is an unknown quantity at this point 
Solved Threads: 1
goody11 goody11 is offline Offline
Junior Poster

Re: Save function not working

 
0
  #6
Jul 5th, 2009
How would I use new and delete?
Reply With Quote Quick reply to this message  
Join Date: Nov 2008
Posts: 941
Reputation: MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice MosaicFuneral is just really nice 
Solved Threads: 92
MosaicFuneral's Avatar
MosaicFuneral MosaicFuneral is offline Offline
Posting Shark

Re: Save function not working

 
0
  #7
Jul 5th, 2009
http://www.cplusplus.com/doc/tutorial/dynamic/

  1. type *var = new type[size];
  2.  
  3. if(!var) return(false); /*or somesort of error handling*/
  4.  
  5. delete [] var;
"Jedenfalls bin ich überzeugt, daß der Alte nicht würfelt."
"I became very sensitive to what will happen to all this and all of us." -Two geniuses named Albert
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,340
Reputation: Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute Ancient Dragon has a reputation beyond repute 
Solved Threads: 1458
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is offline Offline
Still Learning

Re: Save function not working

 
0
  #8
Jul 5th, 2009
Originally Posted by goody11 View Post
How would I use new and delete?
you are kidding aren't you? You mean you don't know diddley-squat about c++ yet you are attempting to write a windows program

  1. if(hFile != INVALID_HANDLE_VALUE)
  2. {
  3. DWORD dwTextLength;
  4.  
  5. dwTextLength = GetWindowTextLength(hEdit);
  6. // No need to bother if there's no text.
  7. if(dwTextLength > 0)
  8. {
  9. char* pszText;
  10. DWORD dwBufferSize = dwTextLength + 1;
  11.  
  12. try
  13. {
  14. pszText = new char[dwBufferSize];
  15. if(GetWindowText(hEdit,pszText, dwBufferSize))
  16. {
  17. DWORD dwWritten;
  18.  
  19. if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
  20. bSuccess = TRUE;
  21. }
  22. delete[] pszText;
  23. }
  24. catch(...)
  25. {
  26. // allocation failed
  27. }
  28. }
Last edited by Ancient Dragon; Jul 5th, 2009 at 3:52 pm.
Reply With Quote Quick reply to this message  
Join Date: Jun 2009
Posts: 104
Reputation: goody11 is an unknown quantity at this point 
Solved Threads: 1
goody11 goody11 is offline Offline
Junior Poster

Re: Save function not working

 
0
  #9
Jul 5th, 2009
It still isn't working and I added everything in.
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC