Solved

Copying files in an MFC app

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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
bobThere java chalenge 45 84
unable to delete all specified values regedit 38 155
format the code in java 6 72
Problem to open Excel file 15 43
This is to be the first in a series of articles demonstrating the development of a complete windows based application using the MFC classes.  I’ll try to keep each article focused on one (or a couple) of the tasks that one may meet.   Introductio…
Introduction: Dialogs (2) modeless dialog and a worker thread.  Handling data shared between threads.  Recursive functions. Continuing from the tenth article about sudoku.   Last article we worked with a modal dialog to help maintain informat…
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.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

758 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

21 Experts available now in Live!

Get 1:1 Help Now