Thread: Save As Dialog not saving correctly

  1. #1
    Registered User
    Join Date
    May 2003
    Posts
    26

    Save As Dialog not saving correctly

    The code below opens the dialog fine and saves fine with the GetFileName function. However, the already existing file (fprint.txt) that I want the Save As dialog to focus on is not being used. A new file is being created instead with out the contents of the existing file. Thanks for the info. Looking forward to a solution.

    Code:
    CString strfileName;
    FILE* stream;
    stream = fopen( "fprintf.txt", "a+");
    	CFileDialog saveFile( FALSE, ".txt", NULL, OFN_HIDEREADONLY | OFN_OVERWRITEPROMPT,
    						"Text file (*.txt)|*.txt|" , NULL );
    	saveFile.m_ofn.lpstrTitle = "Save file as...";
    	
    	if(IDOK == saveFile.DoModal())	{		
    		strfileName = saveFile.GetFileName();
    		fprintf(stream, strfileName);
    }
    James
    Last edited by jkw2068; 08-07-2003 at 01:50 PM.

  2. #2
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Where's the fopen( )?
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  3. #3
    Registered User
    Join Date
    May 2003
    Posts
    26
    Originally posted by XSquared
    Where's the fopen( )?
    stream = fopen( "fprintf.txt", "a+");

    Is this not correct maybe?

  4. #4
    Registered User
    Join Date
    May 2003
    Posts
    26
    My last few recent post have been aound this subject specifically, I am unable to located good MFC tutorials. Thanks for the patience.

  5. #5
    C++ Developer XSquared's Avatar
    Join Date
    Jun 2002
    Location
    Ontario, Canada
    Posts
    2,718
    Sorry 'bout that. I'm a bit blind today.
    Naturally I didn't feel inspired enough to read all the links for you, since I already slaved away for long hours under a blistering sun pressing the search button after typing four whole words! - Quzah

    You. Fetch me my copy of the Wall Street Journal. You two, fight to the death - Stewie

  6. #6
    Registered User
    Join Date
    May 2003
    Posts
    26
    No solution yet.

  7. #7
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    Why are you using fopen() and fprintf() in a MFC (or C++) program?

    MFC has its own File class, CFile.


    You seem to want fprintf() to copy one file to another.
    It does not do this.


    to debug your app;

    What value does

    fprintf(stream, strfileName);

    return? (should be the bytes written to the file which will be zero as you are using it incorrectly)


    what does

    strfileName

    contain at this point?
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  8. #8
    Registered User
    Join Date
    May 2003
    Posts
    26
    >>Why are you using fopen() and fprintf() in a MFC (or C++) >>program?

    I began to learn the functions of the MFC CFile classes after I applied this. I plan on revamping it all once I get the familiarities of MFC down.

    >>You seem to want fprintf() to copy one file to another.
    >>It does not do this.

    Exactly, only using the CFile.Dialog to simplify the process for the user.


    >>to debug your app; What value does fprintf(stream,>>strfileName); return? (should be the bytes written to >>the file which will be zero as you are using it incorrectly)

    0 is correct. so aggravating


    >>what does strfileName contain at this point?

    Not sure what you mean, 'strfileName' contains the information gathered by the user during the GetFileName function.

    Thank you much for your reply.

  9. #9
    train spotter
    Join Date
    Aug 2001
    Location
    near a computer
    Posts
    3,868
    >>fprintf(stream, strfileName);

    What do you think this line does? What is this line purpose?



    >>0 is correct. so aggravating

    NO

    zero means it failed to copy anything to 'stream'. Look up the return value of fprintf() in the help.

    Why are you not testing that the file was opened?

    FILE* stream=NULL;
    stream = fopen( "fprintf.txt", "a+");
    if(stream==NULL)
    iError=GetLastError(); //find out why and handle fail


    fprintf(stream,"%s",sText); //this will print the contents of a string to an open file.

    Look for help on format specifiers (format control string) for the printf functions.

    >>what does strfileName contain at this point?

    Does it contain the correct file name and path? Use the debugger (and a break point) to look at its contents during run time.

    You can used the debugger can't you? If not learn NOW.

    >>fopen()
    Superceded by CreateFile() and CFile class
    Last edited by novacain; 08-11-2003 at 09:48 PM.
    "Man alone suffers so excruciatingly in the world that he was compelled to invent laughter."
    Friedrich Nietzsche

    "I spent a lot of my money on booze, birds and fast cars......the rest I squandered."
    George Best

    "If you are going through hell....keep going."
    Winston Churchill

  10. #10
    Registered User
    Join Date
    May 2003
    Posts
    26
    To answer the majority of your questions

    Originally posted by jkw2068
    I plan on revamping it all once I get the familiarities of MFC down.

    What I would like to see is a working model example of this function. It doesn't need to be based of off my code.


    Appreciate the direction Novacain.
    Last edited by jkw2068; 08-12-2003 at 09:50 AM.

  11. #11
    Registered User
    Join Date
    May 2003
    Posts
    26
    Solved. I will try to post the outcome shortly. Thanks for the help. FINE.

    James

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Save Dialog
    By Tropicalia in forum Windows Programming
    Replies: 6
    Last Post: 10-05-2006, 06:07 PM
  2. Have a new dialog replace another one
    By axr0284 in forum Windows Programming
    Replies: 6
    Last Post: 03-10-2006, 05:23 PM
  3. Edit controls of a dialog from another dialog...
    By Snake in forum Windows Programming
    Replies: 9
    Last Post: 07-01-2005, 02:18 PM
  4. MFC Save As dialog
    By Micko in forum Windows Programming
    Replies: 0
    Last Post: 08-23-2004, 02:02 PM
  5. edit control in dialog box problems
    By Bajanine in forum Windows Programming
    Replies: 11
    Last Post: 11-11-2002, 06:55 PM