Solved

How to use CFile::Duplicate ?

Posted on 1998-10-02
6
1,623 Views
Last Modified: 2013-11-20
How to use CFile::Duplicate ?
I always fail to use it.
Any sample code would be appreciated.
Thanx in advance
0
Comment
Question by:nightingale
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
6 Comments
 
LVL 8

Expert Comment

by:MikeP090797
ID: 1322867
CFile *fl=m_File.Duplicate();


0
 

Author Comment

by:nightingale
ID: 1322868

Yes , I wrote the same code.

After that I wrote

fl->Flush();
fl->Close();
delete  fl;

The error report the file fl with no name

Would you mind writing me in detail?
0
 
LVL 8

Expert Comment

by:Answers2000
ID: 1322869
CFile::Duplicate is equivalent to the runtime library _dup (look this up in the help it has a nice example)

The reason for using this is to re-assign one of the standard file handles (stdin, stdout, stderr) to a new CFile variable.

For any other file you shouldn't call Duplicate.  You should simply copy the pointer

CFile * pFile2 = &m_File ;

// Now work with pFile2

Remember though if you do this, you aren't duplicating the fact that the file is already open, just the pointer to the same C++ object.  Therefore you should only work with one pointer/C++ object to the file at a time.

// this is okay
CFile * pFile2 = &m_File ;
pFile2->Flush() ;
pFile2->Close() ;

// this is okay
CFile * pFile2 = &m_File ;
m_File.Flush() ;
m_File.Close() ;

// this is NOT okay
pFile2->Flush() ;
m_File.Flush() ; // <--- equivalent to flushing the same file twice
m_File.Close() ;
pFile2->Close() ; // <-- now closing the same file for a second time, arghh!!;

0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:nightingale
ID: 1322870

Luckily to hear from you, Answers2000
Thank for your patient, and explaining so carefully

something still unclear is


I donot understand why  you said

this is to re-assign one of the standard file handles (stdin, stdout, stderr) to a new CFile variable.

There is no document show that

//I want to write such code which finish COPY command
---------------------------------------------------------

      char * pFileName1="c:\\temp\\qq1";
      char * pFileName2="c:\\temp\\qq2";
      CFile *p1,*p2;
      p1 =new CFile(pFileName1, CFile::modeRead);
      p2= p1 ->Duplicate();             <--------------does not work
      p2->SetFilePath(pFileName2);
      p2->Flush();
      p2->Close();
      delete p2;  //because in CFile:Duplicate , CFile* pFile = new CFile(hFileNull);
      p1 -> Close();


// instead of wrote code like that
--------------------------------------------------

TCHAR sBuffer[1024];                <-------------- works fine
while (sActual=p1->Read(sBuffer,1023)
  {
    p2 -> Write(sBuffer, sActual);

  }







Here is prototype of CFile:Duplicate()
//-------------------------------------------
CFile* CFile::Duplicate() const
{
      ASSERT_VALID(this);
      ASSERT(m_hFile != (UINT)hFileNull);

      CFile* pFile = new CFile(hFileNull);
      HANDLE hFile;
      if (!::DuplicateHandle(::GetCurrentProcess(), (HANDLE)m_hFile,
            ::GetCurrentProcess(), &hFile, 0, FALSE, DUPLICATE_SAME_ACCESS))
      {
            delete pFile;
            CFileException::ThrowOsError((LONG)::GetLastError());
      }
      pFile->m_hFile = (UINT)hFile;
      ASSERT(pFile->m_hFile != (UINT)hFileNull);
      pFile->m_bCloseOnDelete = m_bCloseOnDelete;
      return pFile;
}


//Doc in Help
---------------------------------
CFile::Duplicate
virtual CFile* Duplicate( ) const;
throw( CFileException );

Return Value

A pointer to a duplicate CFile object.

Remarks

Constructs a duplicate CFile object for a given file.
 This is equivalent to the C run-time function _dup.



Thanx, Waiting you

0
 
LVL 8

Accepted Solution

by:
Answers2000 earned 50 total points
ID: 1322871
In my docs for CFile::Duplicate (MS VC v5) it says _dup and CFile::Duplicate are equivalent.

The code you list for CFile::Duplicate calling DuplicateHandle proves this.

Why would this matter ?
Because if you consider a (for example) command line process there are some standard file handles (stdin, stdout, stderr).  This are normally hooked into certain output directions (the console, the keyboard and the console respectively).  These file handles can be redirected for command line programs using the various piping and redirection symbols (e.g. |, >, <, >>).

CFile::Duplicate and _dup C function are used to do this kind of redirection even when you get a command line which doesn't tell you to redirect.

Why would you want to do this ?

Consider a program which takes a command line option and can output to several places.  Where the output is redirected may have to be calculated at runtime (e.g. depending on the result of some calculation).  The user will not know how to redirect from the command line, so the program will have decide for itself, hence CFile::Duplicate.


Now what about copying a file ?

Look at CopyFile and CopyFileEx APIs (Okay to call these from an MFC program)

BOOL bWorked = CopyFile( "C:\OLDFILE.TXT", "C:\NEWFILE.TXT", FALSE ) ;

The last param of CopyFile says what to do if the output file already exists (whether it should be overwritten or the CopyFile should fail).


KEY POINT
Now the point to remember for a CFile object is that it does NOT represent a File on your disk or else where, it simply represents the "Access Mechanism" to a file.  By duplicating  this "Access Mechanism" in an arbitary way you are confusing the operating system (and hence also the runtime library including MFC).

If you consider my previous explanation, and think of CFile objects as "Access Mechanisms to Files" rather than the files themselves, this should make sense
0
 

Author Comment

by:nightingale
ID: 1322872
Very much Thanks
I just want to use Duplicate to copy file from URL
0

Featured Post

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this article, I'll describe -- and show pictures of -- some of the significant additions that have been made available to programmers in the MFC Feature Pack for Visual C++ 2008.  These same feature are in the MFC libraries that come with Visual …
Introduction: Dynamic window placements and drawing on a form, simple usage of windows registry as a storage place for information. Continuing from the first article about sudoku.  There we have designed the application and put a lot of user int…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…

734 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question