CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 15 of 17

Hybrid View

  1. #1
    sunnysky Guest

    How Do I Still Use Document/View Architecture for This Application?

    Hello,

    I am learning to use MFC to code a small project to read and display csv files. I can't use CArchive to correctly read line by line of a text file as pointed out in this post: http://connect.microsoft.com/VisualS...s-chinese-text . So I decided to use CStdioFile as described here http://forums.codeguru.com/showthrea...e-line-by-line . So this means that I can't use Serialize() function of the document class and I need to handle ID_FILE_OPEN message other than CWinApp::OnFileOpen. So where shall I handle ID_FILE_OPEN message now and how can I still comply with the Document/View Architecture?

    Thanks,
    Brian

  2. #2
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How Do I Still Use Document/View Architecture for This Application?

    CArchive has a ReadString function that reads lines of text. Looks like it's designed to do what you want. Instead of using the >> operators, just use ReadString.

  3. #3
    sunnysky Guest

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by GCDEF View Post
    CArchive has a ReadString function that reads lines of text. Looks like it's designed to do what you want. Instead of using the >> operators, just use ReadString.
    I have problem using CArchive::ReadString. I have the same problem as the first link in my original post.

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by sunnysky View Post
    I have problem using CArchive::ReadString. I have the same problem as the first link in my original post.
    But there was very clear explained that that the reason was:
    ... use CStringW to read a non-unicode text file in binary mode
    So either use CStringA or use a unicode text file.
    Victor Nijegorodov

  5. #5
    sunnysky Guest

    Re: How Do I Still Use Document/View Architecture for This Application?

    I tried to use CStringA but got an error.

    Code:
    void CCSVDisplayDoc::Serialize(CArchive& ar)
    {
    	if (ar.IsStoring())
    	{
    		// TODO: add storing code here
    	}
    	else
    	{
    		CStringA row;
    		ar.ReadString(row);
    	}
    }
    error C2664: 'BOOL CArchive::ReadString(CString &)' : cannot convert parameter 1 from 'CStringA' to 'CString &'
    How to generate a unicode text file?

  6. #6
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by sunnysky View Post
    How to generate a unicode text file?
    The simplest way - using Notepad. Open text file, then Save As... and select the Encoding Unicode from the combobox.
    Victor Nijegorodov

  7. #7
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by sunnysky View Post
    I tried to use CStringA but got an error.

    Code:
    void CCSVDisplayDoc::Serialize(CArchive& ar)
    {
    	if (ar.IsStoring())
    	{
    		// TODO: add storing code here
    	}
    	else
    	{
    		CStringA row;
    		ar.ReadString(row);
    	}
    }


    How to generate a unicode text file?

    It would probably make more sense to change your project properties to use MBCS instead of Unicode.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How Do I Still Use Document/View Architecture for This Application?

    Well, I never used CArchive and never had any problem to live without it.
    For text/binary files i usualy use CStdioFile/CFile classes or just a plain API (CreateFile/ReadFile/WriteFile).
    In case of database (like MS Access or SQL Server) - ADO (well, for Access I also used DAO, but it was more than a decade back). You can also use MFC ODBC classes or some other tolls depending on the DBMS you are using.
    Victor Nijegorodov

  9. #9
    sunnysky Guest

    Re: How Do I Still Use Document/View Architecture for This Application?

    Hi Victor,

    When you handle text/binary files with CStdioFile/CFile, where do you usually put the file handlers? Do you override CWinApp::OnFileOpen() to handle ID_FILE_OPEN message? If so, where do you put the message handler?

    Thanks,
    Brian

  10. #10
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How Do I Still Use Document/View Architecture for This Application?

    I usally implemented my own reading in CDocument::OnOpenDocument override
    Victor Nijegorodov

  11. #11
    sunnysky Guest

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by VictorN View Post
    I usally implemented my own reading in CDocument::OnOpenDocument override
    Thanks Victor. So this way still comply with the Document/View architecture. From MSDN, http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx I also found "Override this function if you want to use something other than the archive mechanism or the file mechanism."

    Is it a good practice to call CDocument::OnOpenDocument() at the beginning of the new OnOpenDocument() function?

  12. #12
    GCDEF is offline Elite Member Power Poster
    Join Date
    Nov 2003
    Location
    Florida
    Posts
    12,635

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by sunnysky View Post
    Thanks Victor. So this way still comply with the Document/View architecture. From MSDN, http://msdn.microsoft.com/en-us/libr...v=vs.100).aspx I also found "Override this function if you want to use something other than the archive mechanism or the file mechanism."

    Is it a good practice to call CDocument::OnOpenDocument() at the beginning of the new OnOpenDocument() function?
    Sounds like you need to step through the MFC code and get a better understanding of how it all works.

  13. #13
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How Do I Still Use Document/View Architecture for This Application?

    Quote Originally Posted by GCDEF View Post
    Sounds like you need to step through the MFC code and get a better understanding of how it all works.
    Agree!
    Victor Nijegorodov

  14. #14
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,396

    Re: How Do I Still Use Document/View Architecture for This Application?

    I do NOT call CDocument::OnOpenDocument() if I use my own mechanism instead of CArchive.
    Besides, I usually use MDI rather than SDI.
    Victor Nijegorodov

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