-
July 2nd, 2014, 04:00 PM
#1
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
-
July 2nd, 2014, 05:43 PM
#2
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.
-
July 2nd, 2014, 07:05 PM
#3
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by GCDEF
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.
-
July 3rd, 2014, 01:19 AM
#4
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by sunnysky
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
-
July 3rd, 2014, 08:07 AM
#5
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?
-
July 3rd, 2014, 08:53 AM
#6
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by sunnysky
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
-
July 3rd, 2014, 10:09 AM
#7
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by sunnysky
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.
-
July 3rd, 2014, 10:12 AM
#8
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by VictorN
The simplest way - using Notepad. Open text file, then Save As... and select the Encoding Unicode from the combobox.
Thanks, it works.
-
July 3rd, 2014, 10:21 AM
#9
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by GCDEF
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.
-
July 3rd, 2014, 10:29 AM
#10
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?
-
July 3rd, 2014, 10:42 AM
#11
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
-
July 3rd, 2014, 10:50 AM
#12
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
-
July 3rd, 2014, 11:04 AM
#13
Re: How Do I Still Use Document/View Architecture for This Application?
I usally implemented my own reading in CDocument::OnOpenDocument override
Victor Nijegorodov
-
July 3rd, 2014, 11:29 AM
#14
Re: How Do I Still Use Document/View Architecture for This Application?
Originally Posted by VictorN
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?
-
July 3rd, 2014, 11:46 AM
#15
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
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|