Re: Save text from CList into file

Tech-Archive recommends: Repair Windows Errors & Optimize Windows Performance



Take a look at my Logging ListBox Control. But see below...
On 2 Sep 2006 08:20:00 -0700, "Dalton" <vicorca@xxxxxxxxx> 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@xxxxxxxxxxxx
Web: http://www.flounder.com
MVP Tips: http://www.flounder.com/mvp_tips.htm
.



Relevant Pages

  • Re: Passing parameter (VB6)
    ... > It is just given to you s a string, ... object where the ".exe" extension is assumed. ... Rick - MVP ...
    (microsoft.public.vb.general.discussion)
  • Re: Large text file import: MVP question
    ... througput time than from the basic Net namespace. ... splitting that the normal string is not mutable. ... change, with mid, split, tolower, or whatever will result in a new string. ... that this is an MVP question. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Open files from a file register
    ... The filesystemobject makes it fairly simple to determine the file extension ... Sub OpenDoc() ... Dim strFullName As String ... I want to be able to open the selected file - I am using some code in ...
    (microsoft.public.excel.programming)
  • Re: Running other applications
    ... associated with a file type (extension). ... file and calling FindExecutable: ... ByVal lpDirectory As String, ByVal lpResult As String) As Long ... > The app is a program called Data Junction. ...
    (microsoft.public.vb.general.discussion)
  • Re: CCertAdmin.SetCertificateExtension
    ... IntPtr instead of string. ... > CCertAdminClass admin = new CCertAdminClass; ... > But the problem is that you pass string as a value to an extension ... When the certificate is issued, ...
    (microsoft.public.platformsdk.security)