Solved

How to use CFile::Duplicate ?

Posted on 1998-10-02
6
1,589 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
Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

 

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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Best tools for Backup and restore of Wordpress site 6 57
post4 challenge 28 102
scoreUp challenge 14 63
wordmultiple challenge 12 136
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: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
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.
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

832 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