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

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

Saving to a file MFC

Reply to Message Icon

Name: MrSteve
Date: December 29, 2003 at 21:49:23 Pacific
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.



Sponsored Link
Ads by Google

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

========================================================
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


0

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

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);


0

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

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


0

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

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)


0

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

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
}


0

Related Posts

See More



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

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


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Programming Forum Home


Sponsored links

Ads by Google


Results for: Saving to a file MFC

Writing to a file in C www.computing.net/answers/programming/writing-to-a-file-in-c/10521.html

Linking Hashtable to a File www.computing.net/answers/programming/linking-hashtable-to-a-file/11811.html

how to write character to a file? www.computing.net/answers/programming/how-to-write-character-to-a-file/4564.html