Solved

Copying files in an MFC app

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

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
deburging in oracle form 12 98
ASCII Non-Printable characters/codes and their HTML equivalents 6 114
sum67 challenge 35 97
isEverywhere  challenge 19 75
Introduction: Ownerdraw of the grid button.  A singleton class implentation and usage. Continuing from the fifth article about sudoku.   Open the project in visual studio. Go to the class view – CGridButton should be visible as a class.  R…
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
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.

786 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