How to rename a file to what user has typed into the 'SaveAs" dialog box?

Discussion in 'C' started by metamofia, Nov 19, 2009.

  1. metamofia

    metamofia New Member

    Joined:
    Sep 28, 2009
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    What i want to know is how do i rename an 'old' file into what the user have typed to be saved as.

    For example, in my case i automatically duplicate a .bmp file named 'New.bmp' whenever the user open an image. so after editing the image and all that, the user wants to save the new image and he or she can type her own file name such as john.bmp to be saved as. so what happened is this modified image goes to New.bmp(duplciated one) and when i check the location of the files, it create both New.bmp AND john.bmp but for john.bmp it has 0 bytes.

    Can someone pls advise me on whats wrong? my code is posted below.

    Code:
    void CLEO_MedivisionView::OnFileSaveas32819()
    {
    	// TODO: Add your command handler code here
    
    	FILE			*fs;
    	errno_t			err;
    	static			CString		szFilter;
    	char			hdr[3];
    	char			String[30];
    	int				i;
    	long			position = 0;
    	unsigned short	sav_data;
    	
    
    	szFilter = "BMP (*.bmp)|*.bmp||";
    	
    	CFileDialog FileSavedlg(FALSE, LPCTSTR("bmp"), NULL, OFN_OVERWRITEPROMPT | OFN_HIDEREADONLY, LPCTSTR(szFilter));
    
    	if (FileSavedlg.DoModal() == IDOK) {
    		pen					= false;
    		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) {
    			printf("Cannot open binary file. ");
    			fclose(fs);
    		}
    
    
    		else {
    //****************************************************************************************
    			if (bmp_count == 24){
    				if (no_of_rows == 256 && no_of_cols == 256) {
    					for (i=0; i<256*256; i++) {
    						fputc (image256[i], fbmap2);
    						fputc (image256[i], fbmap2);
    						fputc (image256[i],fbmap2);
    					
    					}
    				}	
    				else if (no_of_rows == 512 && no_of_cols == 512) {
    					for (i=0; i<512*512; i++) {				
    						fputc (image[i], fbmap2);
    						fputc (image[i], fbmap2);
    						fputc (image[i], fbmap2);
    					}
    				}
    			}
    			else if (bmp_count == 8){
    				if (no_of_rows == 256 && no_of_cols == 256) {
    					for (i=0; i<256*256; i++) {	
    						fputc (image256[i], fbmap2);
    					}
    				}	
    				else if (no_of_rows == 512 && no_of_cols == 512) {
    					for (i=0; i<512*512; i++) {
    						fputc (image[i], fbmap2);
    					}
    				}
    			}
    		}	
    
    //****************************************************************************************
    		
    		fclose(fbmap2);
    	
    
    		system("copy New.bmp one.bmp");
    		remove("New.bmp");
    	
    	}
    }
     
    Last edited: Nov 19, 2009
  2. shabbir

    shabbir Administrator Staff Member

    Joined:
    Jul 12, 2004
    Messages:
    15,375
    Likes Received:
    388
    Trophy Points:
    83
    See what is contained in the filename variable
     
  3. metamofia

    metamofia New Member

    Joined:
    Sep 28, 2009
    Messages:
    47
    Likes Received:
    0
    Trophy Points:
    0
    can u please a little further? thanks
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice