| | |
Windows programming - C - Save file function
Thread Solved
![]() |
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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...
Thanks in advance...
C++ Syntax (Toggle Plain Text)
BOOL SaveFile(HWND owner,HWND hEdit, LPCTSTR pszFileName) { HANDLE hFile; BOOL bSuccess = FALSE; MessageBox(owner,pszFileName,0,0); // Testing if the parameter from pszFileName has been recieved // Create the file (overwrite existing files) and assign the handle to hFile hFile = CreateFile(pszFileName, GENERIC_WRITE, 0, NULL, CREATE_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL); //########## RETURNS INVALID HANDLE VALUE ############ //###### MAYBE PROBLEM WITH CREATING THE FILE ######## //#################################################### // If the handle to the file has been created sucessfully if(hFile != INVALID_HANDLE_VALUE) { DWORD dwTextLength; // Get text length of edit control within child window for allocating memory dwTextLength = GetWindowTextLength(hEdit); // If there is no text then there is nothing to write to the file that has been created if(dwTextLength > 0) { LPSTR pszText; // Amount of me mory to be allocated = textsize + 1 for null terminator DWORD dwBufferSize = dwTextLength + 1; // Allocate memory for the text pszText = GlobalAlloc(GPTR, dwBufferSize); if(pszText != NULL) { // Get the Text to be saved to the file fromt he edit control // within the child window. if(GetWindowText(hEdit, pszText, dwBufferSize)) { DWORD dwWritten; // Write the text to the file if(WriteFile(hFile, pszText, dwTextLength, &dwWritten, NULL)) bSuccess = TRUE; //Set Title of child window to the filename SetWindowText(owner, pszFileName); MessageBox(owner,"File saved",0,0); // Using to find error } else { MessageBox(owner,"Could not retrieve text",0,MB_ICONERROR); // Using to find error } // Free memory allocated for text GlobalFree(pszText); } else { MessageBox(owner,"Could not allocate memory",0,MB_ICONERROR); // Using to find error } } else { MessageBox(owner,"No text",0,0);// Using to find error } // Close the handle to the file if it exists CloseHandle(hFile); } else { MessageBox(owner,"Invalid handle value",0,MB_ICONERROR); // Using to find error } return bSuccess; }
Thanks in advance...
Why dont you use the GetLastError Function to get the detailed error number? Then you will be able to find why it is failing.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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.
•
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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
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 ?
C++ Syntax (Toggle Plain Text)
//SaveFile Function... BOOL SaveFile() { //Processing... } //SaveFileAs function BOOL SaveFileAs() { OPENFILENAME ofn; //initialise the ofn structure char szFile[MAX_PATH]; ofn.lpstrFile = szFile; //....Initialise rest of structure... //Pass the structure into GetSaveFileName //Then call SaveFile function with the filename specified by the user SaveFile(....szFile); }
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 ?
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. •
•
Join Date: Aug 2005
Posts: 188
Reputation:
Solved Threads: 3
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.
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,
If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
C++ Syntax (Toggle Plain Text)
char szFile[MAX_PATH] = ""; ofn.lpstrFile = szFile; //....Initialise rest of structure... //Pass the structure into GetSaveFileName
If that does not work, I advice you to post the full code of the SaveFile and SaveFileAs functions.
![]() |
Other Threads in the C++ Forum
- Previous Thread: reading a key without stoping the loop
- Next Thread: find strings in file.txt
| Thread Tools | Search this Thread |
ace_thread api array based binary bitmap borland c++ c/c++ calling char class classes code coding compile console conversion count delete delete-line deploy desktop developer directshow dll download dynamic dynamiccharacterarray email embedded encryption error file forms fstream function functions game givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream information input int integer java lib linkedlist linker loop looping loops map math mathhomeworkhelp matrix memory multiple news node number numbertoword output parameter pointer problem program programming project python random read recursion reference reverse richedit rpg string strings temperature template test text text-file tree url variable vector video win32 windows winsock word wordfrequency wxwidgets






