Computing.Net > Forums > Programming > Saving to a file MFC

Saving to a file MFC

Reply to Message Icon

Original Message
Name: MrSteve
Date: December 29, 2003 at 21:49:23 Pacific
Subject: Saving to a file MFC
OS: XP home
CPU/Ram: 1.9
Comment:

Hello everyone i'm a "noob" to MFC programming. I created a dialog based application, and i want to do 2 things with it, first i have the user fill in a bunch of edit boxes and other things that manipulate variables. How do i make it save like a normal windows app so i can load it later and all the variables would be loaded, and changed and updated appropiately. I know how to do ofstream not really ifstream, but thats not really what i want. I want that default Save As dialog most GUIs have. Thanks in advance for any help you guys provide.


Report Offensive Message For Removal


Response Number 1
Name: borelli35
Date: December 30, 2003 at 15:33:04 Pacific
Reply: (edit)

========================================================
You indicate MFC but not the language so specific examples are not possible right now. You have two choices. First, as you have guessed, save all that data to a file (it doesn't really matter too much what method you use as long as you read it back the same way you wrote it) and then when the program starts it can check for the files existence and if it exists it can then initialize those variable before displaying the dialog box. Your second option is to save these values to the registry and then read the same values back (if they exist) and initialize your variables in that manner. The only real difference is the speed of loading the values but unless your program has an exceptionally large number of values to keep track of, the difference will be negligable.

borelli35


Report Offensive Follow Up For Removal

Response Number 2
Name: egkenny
Date: December 30, 2003 at 15:57:27 Pacific
Reply: (edit)

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

bOpenFileDialog = TRUE for "File Open" or FALSE for "Save As" dialog box
lpszDefExt = default file extension
lpszFileName = current file name
dwFlags = customization flags
lpszFilter = filters for File List box
pParentWnd = pointer to parent window

Example for File Open:

CFileDialog filedlg (TRUE, "*.BMP", "*.BMP",
OFN_FILEMUSTEXIST | OFN_NOREADONLYRETURN | OFN_LONGNAMES,
"Bitmap Files (*.BMP)|*.bmp|Bitmap Files (*.DIB)|*.DIB||", NULL);

Example for Save As:

CFileDialog filedlg (FALSE, "*.BMP", "*.BMP",
OFN_HIDEREADONLY OFN_NOREADONLYRETURN | OFN_LONGNAMES,
"Bitmap Files (*.BMP)|*.bmp|Bitmap Files (*.DIB)|*.DIB||", NULL);


Report Offensive Follow Up For Removal

Response Number 3
Name: MrSteve
Date: December 30, 2003 at 17:47:39 Pacific
Reply: (edit)

Actually i'm using C++ i didn't try the second reply yet so if that works i'll reply back if you don't see a reply i still need help


Report Offensive Follow Up For Removal

Response Number 4
Name: MrSteve
Date: December 30, 2003 at 18:15:20 Pacific
Reply: (edit)

Ok i put this in my function for when you click save and it gave me these errors

Compiling resources...
Compiling...
StdAfx.cpp
Compiling...
BFArmoryWG.cpp
BFArmoryWGDlg.cpp
C:\Steve's Projects\BFArmoryWG\BFArmoryWGDlg.cpp(309) : error C2275: 'BOOL' : illegal use of this type as an expression
c:\program files\microsoft visual studio\vc98\include\windef.h(142) : see declaration of 'BOOL'
C:\Steve's Projects\BFArmoryWG\BFArmoryWGDlg.cpp(309) : error C2146: syntax error : missing ')' before identifier 'bOpenFileDialog'
C:\Steve's Projects\BFArmoryWG\BFArmoryWGDlg.cpp(309) : error C2059: syntax error : ')'
Help.cpp
objects.cpp
Projectile.cpp
urrectFeatures.cpp
Generating Code...
Error executing cl.exe.

BFArmoryWG.exe - 3 error(s), 0 warning(s)


Report Offensive Follow Up For Removal

Response Number 5
Name: egkenny
Date: December 30, 2003 at 21:55:45 Pacific
Reply: (edit)

You mean you wanted a perfect working example???

#include <fstream.h>

...

CString filename;
ofstream out;

// declare "Save As" file dialog
CFileDialog filedlg (FALSE, "*.txt", "*.txt",
OFN_HIDEREADONLY | OFN_NOREADONLYRETURN | OFN_LONGNAMES,
"Text Files (*.txt)|*.txt|All Files (*.*)|*.*||", NULL);

// show "Save As" file dialog
if (filedlg.DoModal () == IDOK) // user has chosen a file, so
{
filename = filedlg.GetPathName (); // extract its filename
out.open(filename); // open file
out << "test data" << endl; // write data
out.close(); // close file
}


Report Offensive Follow Up For Removal


Response Number 6
Name: MrSteve
Date: December 31, 2003 at 07:20:25 Pacific
Reply: (edit)

Thanks you ROCK i love you man i love you as much as any straight man can love another man.


Report Offensive Follow Up For Removal






Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: Saving to a file MFC

Comments:

 


  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 
Data Recovery Software




Have you ever used OpenOffice?

Yes, as my main suite.
Yes, occationally.
Yes, but only once.
No, never.


View Results

Poll Finishes In 3 Days.
Discuss in The Lounge