reading and writing a file to be displayed in a dialog box
I have looked all over the internet and i can't really find anything that really explains serialization well enough that i can follow whats going on. I have the sams book that shows how to do it but the tutorial is jsut here is the code now go. Its hard to understand. Plus, I don't want it to be document view. Here is what i'm wanting. I want a dialog box that i can click an add button and then it popup another dialogbox and i enter information into the edit boxes (that part is easy). But i want it to store the input into a file that can be saved. and then be able to on the main dialog type in a keyword or whatever and it search the file and then spit out the results. I know how to do it with ifstream and ofstream but all of my experience is console based and very little with visual. I know its CArchive and CObject and Serialize but again my knowledge of the subject is very narrow. If someone has the time to explain that would be great. Or if there is a tutorial on the subject that explains to the most part each line of the code dealiing with serialization that would be great. thanks again
You can take advantage of serialization in a dialog based app by having a CFormView, which wraps a dialog in a view class with an associated doc. The form view will be your main dialog.
For more on serialization, look at arccore.cpp and doccore.cpp to see the MFC implementation of CDocument and CArchive. In VC++6.0 these are in the Visual Studio folder, in vc98\mfc\src.
In OnOpenDocument (when reading), or OnSaveDocument (when writing) CDocument constructs a CArchive object.
CArchive opens a file, reads or writes from/to the file. It includes several overloaded operators to make coding easier to write data types ( >> and << operators like ofstream and ifstream classes) CDocument then calls Serialize(), which you override in your document class derived from CDocument to read/write the data for your app.
Re: reading and writing a file to be displayed in a dialog box
Originally posted by win32newb
I don't want it to be document view.
Why use MFC serialization? If you are not using the document/view architecture or anything else associated with serialization and if you can do it in another way, then why use serialization?
Originally quoted by win32newb
I know how to do it with ifstream and ofstream but all of my experience is console based and very little with visual.
If you don't want to use serialization then you can use ifstream and ofstream. GUI based apps use them the same way you would use them in a console app. Other options would be CFile or CreateFile with ReadFile and WriteFile. I have compared the speed of the file streams (ifstream,ofstream) against the CFile and Readfile-WriteFile. The file streams were quite a bit faster when dealing with text files.
Thanks guys for your help. I appologize for not being too clear on the post. You have however answered two questions for me. One on the note of serialization and the other on the ifstream and ofstream. I assumed you could use ifstream and ofstream in a gui but just wasn't sure if it would work. That would be great cause i could ifstream and ofstream the data to the variables of the edit controls. I think i'm gonna go the route of serialization though just so i can learn something new. Thanks to the individual that posted the serialized dlg.zip. Your help is greatly appreciated.
OK enough of my rambling,
Win32newb
"Making Windows programs run worse they they already do"
I was looking at the code you gave me, and I understand to the most part what is written. However, how do you make it append to the file instead of overwriting its data? My guess would be a pointer of some sort, just confused.
Thanks again for the help.
Win32newb
I think you should keep one set of data in one file. That makes it more logical. You read data once and initialize variables in the order everything was saved.
If you need several sets, than you would have to have a user making choice what set of data to read. I am sure that you can find a way of doing that either in a loop or maybe by setting internal file pointer. I have never done it before. Maybe someone else will be able to give you more information.
There are only 10 types of people in the world: Those who understand binary and those who do not.
Not sure how to do this. Does a serialized file hold a vectored array? Pardon me if thats not the correct name for it. For example:
std::vector<CString> myvector;
/*************************************************
I'm assuming you an use CString as a type for a vector
*************************************************/
myvector.push_back(string_editboxvalue1);
myvector.push_back(string_editboxvalue2);
/************************************************
then have a search function
*************************************************/
void CMySerializedDlg::SrchData (CString* pStr1, CString *pStr2)
{
int loopcount=0;
Do {
if (strcmp( pStr1, pStr2 )) // compare strings
{
MessageBox("Found String and loading", MB_OK);
m_editboxcontrol.SetWindowText(pStr1);
}
* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.