Solved

Copying files in an MFC app

Posted on 1999-01-23
2
333 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

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Macro to save highlighted rows as a separate file 5 46
has22 challenge 11 106
ShiftLeft challenge 21 88
Problem to App source 6 89
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…
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 …
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.
Finds all prime numbers in a range requested and places them in a public primes() array. I've demostrated a template size of 30 (2 * 3 * 5) but larger templates can be built such 210  (2 * 3 * 5 * 7) or 2310  (2 * 3 * 5 * 7 * 11). The larger templa…

679 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