CFile vs CStdioFile problem writh reading / writing text 
Author Message
 CFile vs CStdioFile problem writh reading / writing text

Hi,
    I coded the following two examples but only one of them works but they
do exactly the same thing! They write data to a file and then read that
data. I have simplified the code down to just the basics to emphasis my
quandary. Why does code snippet 1 fail but code snippet 2 work fine?? BTW
You can examine the created file for both ways and they are identical down
to a binary level!

// (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();

Quote:
}

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();

Quote:
}

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();

Quote:
}

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();

Quote:
}

delete pFile;

Many thx.
Leigh Firth.



Sun, 30 Jan 2005 09:05:26 GMT  
 CFile vs CStdioFile problem writh reading / writing text

Hi Leigh,

Quote:
> // (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...

Quote:

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



Sun, 30 Jan 2005 10:08:49 GMT  
 CFile vs CStdioFile problem writh reading / writing text
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 =)

                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.



Quote:

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



Sun, 30 Jan 2005 14:02:46 GMT  
 CFile vs CStdioFile problem writh reading / writing text

Quote:

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

Quote:
>                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



Sun, 30 Jan 2005 20:51:42 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Writing bold to .rtf - CFile or CStdioFile or ?

2. Are CFile::Read() and CFile::Write() Really Unbuffered?

3. Problem with write using CFile and read using fscanf()

4. CFile::Read - text file problem

5. Beginner Q: Reading all text from a CStdioFile into a CString

6. problem reading/writing text file from disk

7. CFile Write and Read for all apps

8. Reading from text files using CFile object

9. CFile: reading a line in a text file

10. CFile-- reading text, binary, and etc?????

11. Reading a multi-line text file with a CFile object

12. Problem using CStdioFile to read file

 

 
Powered by phpBB® Forum Software