Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Rename file c++ (http://www.go4expert.com/forums/rename-file-cpp-t20144/)

metamofia 20Nov2009 07:24

Rename file c++
Hi all,

Im using MFC to create my application and im stuck with the whole 'save as' function.

Im stuck with renaming an existing file to what the user keyed in the dialog box of 'save as'.

for example i got a file new.bmp.
so after editing the image and all that, the user wants to save it as 'john'
i already have this code:

(FileSavedlg.DoModal() == IDOK) {
pen =
man_segment_select =
false; //Don't set the flag for Selecting region manually for Segmentation
const size_t StringSize = 100;
size_t CharactersConverted = 0;
char filename[StringSize];
wcstombs_s(&CharactersConverted, filename, FileSavedlg.GetFileName().GetLength()+1, FileSavedlg.GetFileName(), _TRUNCATE);
if ((err = fopen_s(&fs, filename, "wb+")) != 0) {
"Cannot open binary file. ");

when i do this: rename(new.bmp, filename);
it gives me a filename called 'filename' with an unknown type of file but with the same size as 'new.bmp'. and then there's another file called john in BMP format BUT its empty. why is this so? where have i go wrong in this? if u need my full codes for the 'save as' function pls do let me know. help will be appreciated.

Gene Poole 20Nov2009 08:43

Re: Rename file c++
It's not at all clear what you are doing, but why the wcstombs_s? Just use _wfopen_s, or better yet, open a CFile and avoid all the string conversion nonsense; fopen_s is going to have to convert it back to wide char before calling the API internally anyway. Also, the windows API call to MoveFile is probably a better way to rename files in MFC and _UNICODE.

All times are GMT +5.5. The time now is 08:42.