CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 12 of 12
  1. #1
    Join Date
    Mar 2004
    Posts
    13

    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

  2. #2
    Join Date
    Sep 1999
    Location
    Colorado, USA
    Posts
    1,002
    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.

    Hope it helps.
    Steve

  3. #3
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    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?

    Also see How to enhance a dialog-based application with Menu, Toolbar...?
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  4. #4
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917

    Re: Re: reading and writing a file to be displayed in a dialog box

    Originally posted by Sam Hobbs
    Why use MFC serialization? [/URL]
    Why not if so desired?
    Attached Files Attached Files
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  5. #5
    Join Date
    May 1999
    Location
    Southern California
    Posts
    12,266

    Re: Re: Re: reading and writing a file to be displayed in a dialog box

    Originally posted by JohnCz
    Why not if so desired?
    It sounds as if win32newb does not desire to use serialization. The question asks how to use it, but I get the impression it is not desired.
    "Signature":
    My web site is Simple Samples.
    C# Corner Editor

  6. #6
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    You are right. It depends on how one interprets that post. Maybe I have drawn wrong conclusion about desire.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  7. #7
    Join Date
    Jan 2004
    Location
    Earth
    Posts
    567
    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.

    TDM

  8. #8
    Join Date
    Mar 2004
    Posts
    13
    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"

  9. #9
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    Originally posted by win32newb
    Thanks to the individual that posted the serialized dlg.zip.
    win32newb, you are welcome.
    I am not an individual. We carry user names and you can address me by JohnCz or simply John.
    There are only 10 types of people in the world:
    Those who understand binary and those who do not.

  10. #10
    Join Date
    Mar 2004
    Posts
    13

    John I have a question

    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

  11. #11
    Join Date
    May 1999
    Location
    ALABAMA, USA
    Posts
    9,917
    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.

  12. #12
    Join Date
    Mar 2004
    Posts
    13

    Puzzled

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

    else

    {
    MessageBox("No Match Found",MB_0K);
    }

    loopcount++;
    }while (loopcount<myvector.size() );
    }

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured