Solved

Copying files in an MFC app

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
mixString challenge 36 131
windows 10 how make picture as desktop background 2 65
noX challenge 17 129
mapBully challenge 6 152
Here is how to use MFC's automatic Radio Button handling in your dialog boxes and forms.  Beginner programmers usually start with a OnClick handler for each radio button and that's just not the right way to go.  MFC has a very cool system for handli…
Have you tried to learn about Unicode, UTF-8, and multibyte text encoding and all the articles are just too "academic" or too technical? This article aims to make the whole topic easy for just about anyone to understand.
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.
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…

839 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