Solved

How to use CFile::Duplicate ?

Posted on 1998-10-02
6
1,575 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
  • 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
Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

 

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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Login Script to Copy Folders 12 21
Pressing cancel button with C# UI Automation 3 40
List out all word 7 224
zeroMAx challenge 20 76
Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.
Excel styles will make formatting consistent and let you apply and change formatting faster. In this tutorial, you'll learn how to use Excel's built-in styles, how to modify styles, and how to create your own. You'll also learn how to use your custo…

706 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

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now