Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 4614996
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T01:46:38+00:00 2026-05-22T01:46:38+00:00

I am writing an MFC C++ application that has a Save As button for

  • 0

I am writing an MFC C++ application that has a Save As button for saving a .txt file to the disc. With it I am trying to add an extra verification for file overwriting (if a file with the same filename exists, then it should query the user if he wants to overwrite the old file or not). I have tried this with the below code, but it doesn’t really work. When I click No on the MessageBox, it should reopen the Save As file dialog, but instead it gives me two errors: the first one is Debug assertion failed, and the second one is Encountered an improper argument. How should I do this better? This is the code:

char strFilter[] = { "Text Files (*.txt)|*.txt|" }; 

    CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter)); 

    while(true)
    {
        if( FileDlg.DoModal() == IDOK ) // this is the line which gives the errors
        {
            agendaName = FileDlg.GetFileName(); //filename
            agendaPath = FileDlg.GetFolderPath(); //filepath (folders)

            if(model->agendaExists(CSToString(agendaPath+TEXT("\\")+agendaName))) // there is another file called the same way
            {
                if(MessageBox(TEXT("A file with the specified name already exists. Overwrite?"), TEXT("File exists"), MB_YESNO) != 6) // user clicked NO (do not overwrite file)
                {
                    continue;
                }

            }

            model->sendToFile(CSToString(agendaPath+TEXT("\\")+agendaName));  // the file is unique so the agenda named agendaName found at path agendaPath is saved
            return;
        }
    }

It should be mentioned that the errors occur on line 7 and only on the second loop through the while.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T01:46:39+00:00Added an answer on May 22, 2026 at 1:46 am

    CFileDialog can detect itself if a file exists and prompt the user for overwriting.

    explicit CFileDialog(
       BOOL bOpenFileDialog,
       LPCTSTR lpszDefExt = NULL,
       LPCTSTR lpszFileName = NULL,
       DWORD dwFlags = OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
       LPCTSTR lpszFilter = NULL,
       CWnd* pParentWnd = NULL,
       DWORD dwSize = 0
    );
    

    Just pass OFN_OVERWRITEPROMPT for the flags.

    As for your problem, run in Debugger and when you get that assertion press the Retry button to see where the problem comes from (you’ll probably have to look through the call stack also). Maybe you should try putting this in the while loop:

    CFileDialog FileDlg(FALSE, CString(".txt"), NULL, 0, CString(strFilter)); 
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an MFC application that doesn't use .NET (CLR support is set
I am writing a C++/MFC application that will use Microsoft Access to store data.
Writing my first Linq application, and I'm trying to find the best way to
I'm working on a bug where I have an MFC application that does weird
OK, so we're writing our MFC application to make use of the built-in localization
I´m writing an application for Android in which I have a character that I
The software in question is a native C++/MFC application that receives a large amount
Writing my first silverlight application. I need to deliver some bitmap that the customer
Am writing an application for iphone to read a text file using NSData. NSFileHandle
I'm writing an MFC App to automatically configure Postgresql with ODBC for use by

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.