Solved

How to use CFile::Duplicate ?

Posted on 1998-10-02
6
1,641 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
Database Solutions Engineer FAQs

In this series, we will discuss common questions received as a database Solutions Engineer at Percona. In this role, we speak with a wide array of MySQL and MongoDB users responsible for both extremely large and complex environments to smaller single-server environments.

 

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

[Webinar] Learn How Hackers Steal Your Credentials

Do You Know How Hackers Steal Your Credentials? Join us and Skyport Systems to learn how hackers steal your credentials and why Active Directory must be secure to stop them. Thursday, July 13, 2017 10:00 A.M. PDT

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 …
Exception Handling is in the core of any application that is able to dignify its name. In this article, I'll guide you through the process of writing a DRY (Don't Repeat Yourself) Exception Handling mechanism, using Aspect Oriented Programming.
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.
NetCrunch network monitor is a highly extensive platform for network monitoring and alert generation. In this video you'll see a live demo of NetCrunch with most notable features explained in a walk-through manner. You'll also get to know the philos…

615 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