Windows programming - C - Save file function

Thread Solved
Reply

Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Windows programming - C - Save file function

 
0
  #1
Feb 12th, 2006
Hello again everyone, Im having trouble with this function for saving a file from a richedit control within a child window in my application. I have put in extra messageboxes etc to helo me find the error. when I implement this function with valid parameters I recieve the "Invalid handle value" message box so I gathered that there is something wrong creating the handle to the file. However when I use this function for the first time for example when i create a file and save plain text into it, the function creats the file and writes to it successfully, but when i try again to save into this same file I recieve the "Invalid Handle Value" message. Here is the code for the function...

  1. BOOL SaveFile(HWND owner,HWND hEdit, LPCTSTR pszFileName)
  2. {
  3. HANDLE hFile;
  4. BOOL bSuccess = FALSE;
  5.  
  6. MessageBox(owner,pszFileName,0,0); // Testing if the parameter from pszFileName has been recieved
  7.  
  8. // Create the file (overwrite existing files) and assign the handle to hFile
  9. hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL,
  10. CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
  11.  
  12. //########## RETURNS INVALID HANDLE VALUE ############
  13. //###### MAYBE PROBLEM WITH CREATING THE FILE ########
  14. //####################################################
  15.  
  16. // If the handle to the file has been created sucessfully
  17. if(hFile != INVALID_HANDLE_VALUE)
  18. {
  19. DWORD dwTextLength;
  20. // Get text length of edit control within child window for allocating memory
  21. dwTextLength = GetWindowTextLength(hEdit);
  22.  
  23. // If there is no text then there is nothing to write to the file that has been created
  24. if(dwTextLength > 0)
  25. {
  26. LPSTR pszText;
  27. // Amount of me mory to be allocated = textsize + 1 for null terminator
  28. DWORD dwBufferSize = dwTextLength + 1;
  29.  
  30. // Allocate memory for the text
  31. pszText = GlobalAlloc(GPTR, dwBufferSize);
  32. if(pszText != NULL)
  33. {
  34. // Get the Text to be saved to the file fromt he edit control
  35. // within the child window.
  36. if(GetWindowText(hEdit, pszText, dwBufferSize))
  37. {
  38. DWORD dwWritten;
  39.  
  40. // Write the text to the file
  41. if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL))
  42. bSuccess = TRUE;
  43.  
  44. //Set Title of child window to the filename
  45. SetWindowText(owner, pszFileName);
  46. MessageBox(owner,"File saved",0,0); // Using to find error
  47. }
  48. else
  49. {
  50. MessageBox(owner,"Could not retrieve text",0,MB_ICONERROR); // Using to find error
  51. }
  52. // Free memory allocated for text
  53. GlobalFree(pszText);
  54. }
  55. else
  56. {
  57. MessageBox(owner,"Could not allocate memory",0,MB_ICONERROR); // Using to find error
  58. }
  59. }
  60. else
  61. {
  62. MessageBox(owner,"No text",0,0);// Using to find error
  63. }
  64. // Close the handle to the file if it exists
  65. CloseHandle(hFile);
  66. }
  67. else
  68. {
  69. MessageBox(owner,"Invalid handle value",0,MB_ICONERROR); // Using to find error
  70. }
  71. return bSuccess;
  72. }

Thanks in advance...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

 
0
  #2
Feb 12th, 2006
Why dont you use the GetLastError Function to get the detailed error number? Then you will be able to find why it is failing.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

 
0
  #3
Feb 12th, 2006
Oh Thanks, I have found out the error code...32: File cannot be accessed because it is being use by another process. Could this mean I have forgotten to close a handle to a file somewhere? I have checked around for this and still cannot find out why.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

 
0
  #4
Feb 12th, 2006
Just as I thought...Most probably you have opened it using another application. Since it is a Rich-Edit Control, my best guess is that you have opened it using MS-Word and trying to overwrite it while it is open..
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

 
0
  #5
Feb 13th, 2006
That is what I thought as it stated another "Process" ie another application, but it is only being used by my application. The thing is...when i first use the function to save the file it works perfectly, but then when make more changes to the richedit control text and attempt to save it again it does not work...I thought this may be due to forgetting to close a handle to the file somewhere but i cant find any errors.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 15,161
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: 1437
Team Colleague
Featured Poster
Ancient Dragon's Avatar
Ancient Dragon Ancient Dragon is online now Online
Most Valuable Poster

Re: Windows programming - C - Save file function

 
0
  #6
Feb 13th, 2006
If you are running Norton Antivirus turn it off during development. Sometimes it causes problems like that. Don't forget to turn it back on when you are done.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

 
0
  #7
Feb 13th, 2006
In my application I have it so that when i click a save button on the toolbar or menu item it will check to see if the file exists and then decide whether to save the file using the filename from the child window title or create a save as dialog box. The savefileas function creates the dialog box using the OPENFILENAME structure and calling GetSaveFileName, from which i then pass the file specified by the user into my SaveFile function. When I save the file as.. it seems to work every time however, when i call the savefile function say when the file exists it gives this error...ie

  1. //SaveFile Function...
  2. BOOL SaveFile()
  3. {
  4. //Processing...
  5. }
  6.  
  7. //SaveFileAs function
  8. BOOL SaveFileAs()
  9. {
  10. OPENFILENAME ofn;
  11. //initialise the ofn structure
  12. char szFile[MAX_PATH];
  13. ofn.lpstrFile = szFile;
  14. //....Initialise rest of structure...
  15. //Pass the structure into GetSaveFileName
  16.  
  17. //Then call SaveFile function with the filename specified by the user
  18.  
  19. SaveFile(....szFile);
  20.  
  21. }

Therefore this leads me to think that when i am passing the parameters into savefile() when the file exists, something is going wrong...but when the parameters are passed through the saveas dialog box the parameters are passwed correctly. It is confusing...any suggestions ?
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

 
0
  #8
Feb 13th, 2006
It is difficult to point out the cause from here. It will be better if you set a breakpoint just after the call for the GetSaveFileName function and see if all the fields, especially the file name of the OPENFILENAME structure, are filled properly. You better use the ofn.Flags = OFN_OVERWRITEPROMPT; Statement before the GetSaveFileName call if you are not doing so already.
Reply With Quote Quick reply to this message  
Join Date: Aug 2005
Posts: 188
Reputation: bops is an unknown quantity at this point 
Solved Threads: 3
bops bops is offline Offline
Junior Poster

Re: Windows programming - C - Save file function

 
0
  #9
Feb 13th, 2006
Yes I have set the OPENFILENAME structure correctly...It is the process of saving the file using the text from the child window title that does not work correctly...The procedure to save the file as however works fine. The savefileas procedure uses the savefile() function within it so therefore i dont think there is anything wrong with the savefile() function itself...rather the way I am passing parameters to it when not using the saveas procedure.
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 1,496
Reputation: WolfPack has a spectacular aura about WolfPack has a spectacular aura about WolfPack has a spectacular aura about 
Solved Threads: 104
Moderator
WolfPack's Avatar
WolfPack WolfPack is offline Offline
Mentally Challenged Mod.

Re: Windows programming - C - Save file function

 
0
  #10
Feb 13th, 2006
Iam running out of options here. I also think that the SaveFile function is okay. If the filename is correctly returned from GetSaveFileName, the SaveFile function should behave identically in both cases. This is a long shot but try,
  1. char szFile[MAX_PATH] = "";
  2. ofn.lpstrFile = szFile;
  3. //....Initialise rest of structure...
  4. //Pass the structure into GetSaveFileName

If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C++ Forum
Thread Tools Search this Thread



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

©2003 - 2009 DaniWeb® LLC