// (1) DOESN'T WORK!!
CStdioFile* pFile = new CStdioFile();
if(pFile->Open(pDlg->GetPathName(), CFile::modeCreate | CFile::modeWrite |
CFile::shareExclusive))
{
pFile->WriteString("[START]\r\n");
pFile->Close();
}
delete pFile;
CStdioFile* pFile = new CStdioFile();
if(pFile->Open(pDlg->GetPathName(), CFile::modeRead |
CFile::shareDenyWrite))
{
CString sData;
pFile->ReadString(sData);
pFile->Close();
if(sData == "[START]")
{
// Do Something (DOESNT GET HERE AS sData == "[START]\r\n]"
}
pFile->Close();
}
delete pFile;
// (2) WORKS
CFile* pFile = new CFile();
if(pFile->Open(pDlg->GetPathName(), CFile::modeCreate | CFile::modeWrite |
CFile::shareExclusive))
{
pFile->Write("[START]\r\n", 9);
pFile->Close();
}
delete pFile;
CStdioFile* pFile = new CStdioFile();
if(pFile->Open(pDlg->GetPathName(), CFile::modeRead |
CFile::shareDenyWrite))
{
CString sData;
pFile->ReadString(sData);
pFile->Close();
if(sData == "[START]")
{
// Do Something (gets here fine)
}
pFile->Close();
}
delete pFile;
Many thx.
Leigh Firth.
Hi Leigh,
> // (1) DOESN'T WORK!!
> CStdioFile* pFile = new CStdioFile();
>
> if(pFile->Open(pDlg->GetPathName(), CFile::modeCreate | CFile::modeWrite |
> CFile::shareExclusive))
> {
> pFile->WriteString("[START]\r\n");
>
> pFile->Close();
> }
>
> delete pFile;
>
> CStdioFile* pFile = new CStdioFile();
>
> if(pFile->Open(pDlg->GetPathName(), CFile::modeRead |
> CFile::shareDenyWrite))
> {
> CString sData;
> pFile->ReadString(sData);
> pFile->Close();
Here you close pFile...
>
> if(sData == "[START]")
> {
> // Do Something (DOESNT GET HERE AS sData == "[START]\r\n]"
> }
>
> pFile->Close();
...and here you close the file again.
Why are you allocation memory dynamically for those file objects by the
way? There's absolutely no advantage to that.
Why don't you write it like this:
CString strFilename = "C:\\test.txt";
CStdioFile fileOut;
if (fileOut.Open(strFilename, CFile::modeCreate | CFile::modeWrite |
CFile::shareExclusive))
{
fileOut.WriteString("[START]\r\n");
fileOut.Close();
}
CStdioFile fileIn;
if (fileIn.Open(strFilename, CFile::modeRead | CFile::shareDenyWrite))
{
CString sData;
fileIn.ReadString(sData);
fileIn.Close();
if (sData == "[START]")
{
// Do Something (DOESNT GET HERE AS sData == "[START]\r\n]"
}
}
I tested this piece of code and it works.
Hope this helps.
Regards,
Ruben.
As for why I use new and delete, force of habit I guess as I
was once told / read that it is the best way to avoid your exe files from
unnecessary bloat. I prefer small exe's to faster code in most app's I write
and the read and write sections are in different functions actually ;) I
just pasted them next to each other for easier display.
Thx.
Leigh.
"Ruben van Engelenburg" <ru...@NOSPAMPLEASEtextinfo.nl> wrote in message
news:3D5A1E21...@NOSPAMPLEASEtextinfo.nl...
>Hi Ruben,
> That's strange how it works for you, definitely not for me,
>oh well I am just using the way that works anyway, I was just curious =)
Both your CStdioFile and CFile writers write the string literal
"[START]\r\n". However, CStdioFile works in text mode by default and
performs CR/LF translation, and CFile does not. Thus, CStdioFile
actually writes two CRs, the one in your literal plus another when it
translates \n to \r\n. This messes you up later when you read the
string. Note also that CStdioFile::ReadString leaves \n characters in
the string.
> As for why I use new and delete, force of habit I guess as I
>was once told / read that it is the best way to avoid your exe files from
>unnecessary bloat. I prefer small exe's to faster code in most app's I write
>and the read and write sections are in different functions actually ;) I
>just pasted them next to each other for easier display.
You were told wrong. :) Using new/delete can only make your code
bigger and slower and is totally unnecessary for the code you
presented. Moreover, as you're using raw pointers instead of something
like std::auto_ptr, it destroys exception safety.
--
Doug Harrison
Microsoft MVP - Visual C++
Eluent Software, LLC
http://www.eluent.com
Tools for VC++, VS.NET, and Windows