Both you have no idea how helpful you have been.
Joseph M. Newcomer ha escrito:
Take a look at my Logging ListBox Control. But see below...
On 2 Sep 2006 08:20:00 -0700, "Dalton" <vicorca@yahoo.com> wrote:
Hi, guys;
I found a small problem with this. I thought it wasn't important but to
be honest it's becoming a real pain in the ass.
The question is the following:
I've got a Clist with several lines created in the Dialog. At the end
of the dialog, I want to save that text in a file. The code is this:
void CTranslator::OnEditorEnd()
{
// TODO: Add your command handler code here
char szFilter[] = " *.txt";
*****
Forget forever that 'char' is a datatype for common use. It is reserved for very unusual
circumstances, of which this is not one. And your filter syntax is flat-out wrong.
CString Filter(_T("Text Files (*.txt)|*.txt|"));
Better still, this should be a string in your STRINGTABLE resource so it can
internationalized.
****
CFileDialog FileDlg( TRUE, NULL, NULL, OFN_HIDEREADONLY, szFilter);
****
Why NULL for the default extension when you clearly meant _T(".txt")? And the first
parameter is also wrong, see the answer to your other question, below.
****
if (FileDlg.DoModal() == IDOK)
{
try
{
CFile cf(FileDlg.GetPathName() + ".txt", CFile::modeCreate |
CFile::modeWrite);
***
CStdioFile cf;
if(!cf.Open(FileDlg.GetPathName(), CFile::modeCreate |
CFile::modeWrite))
{ /* failed */
... deal with failure here
} /* failed */
That is, first, use CStdioFile. Then do not use the throw-semantics mechanism, but do an
explicit Open. Finally, if you've done a GetPathName, which includes the extension, but
then you add another extension to it. So you might get file.txt.txt as the output
filename!
****
CString text;
cf.Write("\r\n",4);
****
cf.WriteString(_T("\r\n"));
****
for (int a = 0 ; a < line_t ; a++) // line_t is the last line in the
Clist
{
m_cLow.GetText(a, text);
cf.Write(text, text.GetLength());
cf.Write("\r\n", 4);
****
cf.WriteString(text);
cf.Write(_T("\r\n"));
Note that in a Unicode app, text.GetLength() would write only HALF your string; if you are
using CFile::Write, the correct value would be text.GetLength() * sizeof(TCHAR)
****
}
cf.Close();
}
catch (CFileException *e)
{
e->Delete();
MessageBox("Error saving file");
****
You should do a GetLastError or at least extract the actual error code from e so you can
report WHY. See my articles on using FormatMessage in MFC on my MVP Tips site
****
}
}
}
The big problem is that, I don't know why, two blank spaces are added
at the beginning of each line in the file. It's more than an aesthetic
question because the lines saved are part of a code that the reader
doesn't understand when it finds the blank spaces.
****
Because you told it to write 4 of the 2 bytes in the sequence \r\n. The rationale behind
choosing 4 escapes me, and the presumption of 8-bit characters is risky anyway.
****
Another less important question is how I can open a "save Dialog"
instead of a "open dialog", because doing it the way I do, the dialog
is an "open dialog".
***
RTFM? What is the first parameter of the CFileDialog constructor?
joe
****
Joseph M. Newcomer [MVP]
email: newcomer@flounder.com
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm