Solved

Copying files in an MFC app

Posted on 1999-01-23
2
330 Views
Last Modified: 2013-11-20
I need a function in my MFC app to copy files like DOS copy.
It has to create files that are identical in size and time/date. The function I came up with works fine under NT and 98 on my machine, but numerous other people have ran it under 98/95 and received Illegal Operations somewhere around the actual copying stage (their remote with no code/debugger). They only get this when the dir and files already exist. When I take the whole CFileStatus section out, they have no problems (except the timestamp is wrong).
So, anyone know how I can achieve this seemingly simple operation without errors?

void CCopy::Copy(char *in, char *out)
{      
      CFile infile,outfile;
      CFileException e;
      CString Temp;

      if(!infile.Open(in, CFile::modeRead | CFile::typeBinary, &e )) {
            Temp.Format("File could not be opened %s",e.m_cause);
            AfxMessageBox(Temp);
            return; }

      if(!outfile.Open(out, CFile::modeCreate | CFile::modeWrite | CFile::typeBinary, &e )) {
            Temp.Format("File could not be opened %s",e.m_cause);
            AfxMessageBox(Temp);
            return; }

      unsigned int bytes;
      unsigned char buff[4096];

      while((bytes=infile.Read(buff,4096))>0)
            outfile.Write(buff,bytes);

      infile.Close();
      outfile.Close();

      CFileStatus status;

      CFile::GetStatus(in,status);
      
      if(CFile::GetStatus(in,status))
            CFile::SetStatus(out,status);

      return;
}
0
Comment
Question by:forloop
2 Comments
 
LVL 13

Accepted Solution

by:
Epsylon earned 100 total points
ID: 1328221
Use CopyFile API function from winbase.h

BOOL CopyFile(
    LPCTSTR lpExistingFileName,      // pointer to name of an existing file
    LPCTSTR lpNewFileName,      // pointer to filename to copy to
    BOOL bFailIfExists       // flag for operation if file exists
   );      

0
 

Author Comment

by:forloop
ID: 1328222
Thanks, I found the problem with my function to be that the files in the destination folder were read-only. This because they were being copied from a cd-rom. Anyway, thatnks for the API function, it'll make my code better.
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
If you use Adobe Reader X it is possible you can't open OLE PDF documents in the standard. The reason is the 'save box mode' in adobe reader X. Many people think the protected Mode of adobe reader x is only to stop the write access. But this fe…
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.
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

914 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

12 Experts available now in Live!

Get 1:1 Help Now