CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 1 of 2 12 LastLast
Results 1 to 15 of 17
  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 online now 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 online now 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
    sunnysky Guest

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

    Quote Originally Posted by VictorN View Post
    The simplest way - using Notepad. Open text file, then Save As... and select the Encoding Unicode from the combobox.
    Thanks, it works.

  9. #9
    sunnysky Guest

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

    Quote Originally Posted by GCDEF View Post
    It would probably make more sense to change your project properties to use MBCS instead of Unicode.
    Thanks. When I uncheck "Using Unicode Library" at the beginning of the wizard and it works too.

  10. #10
    sunnysky Guest

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

    Just out of curiosity. If we have a file format that are not supported by CArchive, for example a non-standard database file format that need third party access library, and we cannot use CArchive::Serialize() to process reading and writing the file. What is the best practice to design the program architecture?

  11. #11
    VictorN's Avatar
    VictorN is online now 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

  12. #12
    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

  13. #13
    VictorN's Avatar
    VictorN is online now 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

  14. #14
    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?

  15. #15
    VictorN's Avatar
    VictorN is online now 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

Page 1 of 2 12 LastLast

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