1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Rename file c++

Discussion in 'C++' started by metamofia, Nov 20, 2009.

  1. metamofia

    metamofia New Member

    Sep 28, 2009
    Likes Received:
    Trophy Points:
    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 = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2];
    man_segment_select = [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]false[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]; [/SIZE][SIZE=2][COLOR=#008000][SIZE=2][COLOR=#008000]//Don't set the flag for Selecting region manually for Segmentation
    [/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2][/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]const[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] size_t StringSize = 100;
    size_t CharactersConverted = 0;
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]char[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] filename[StringSize];
    wcstombs_s(&CharactersConverted, filename, FileSavedlg.GetFileName().GetLength()+1, FileSavedlg.GetFileName(), _TRUNCATE);
    [/SIZE][SIZE=2][COLOR=#0000ff][SIZE=2][COLOR=#0000ff]if[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2] ((err = fopen_s(&fs, filename, [/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"wb+"[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2])) != 0) {
    printf([/SIZE][SIZE=2][COLOR=#a31515][SIZE=2][COLOR=#a31515]"Cannot open binary file. "[/COLOR][/SIZE][/COLOR][/SIZE][SIZE=2]);
    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.
    Last edited: Nov 20, 2009
  2. Gene Poole

    Gene Poole New Member

    Nov 10, 2009
    Likes Received:
    Trophy Points:
    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.

Share This Page