Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. Best way to duplicate a file?

Best way to duplicate a file?

Scheduled Pinned Locked Moved C / C++ / MFC
helpquestion
8 Posts 6 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    Sousch
    wrote on last edited by
    #1

    Hello, I'm having a problem when I try to save a file in a different directory (let's say that I simply want to duplicate the file). I have a "default.wav" in my directory and I use a dialog to save it (duplicate it) in another directory with a different name. This is my code: ============ CString strFile; (...) strFile = dlg.GetPathName(); (...) CFile file_orig,file_dest; char *pBuffer; (...) <- part that creates "default.wav" in the same directory file_orig.Open("default.wav", CFile::modeRead); int length = file_orig.GetLength(); // <<<<< here is the problem !! pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); free(pBuffer); ============== Any solution that works can be valid (without including aditional libraries if possible). I just want to keep it as simple as possible. Thanks in advance! :)

    R M C S 4 Replies Last reply
    0
    • S Sousch

      Hello, I'm having a problem when I try to save a file in a different directory (let's say that I simply want to duplicate the file). I have a "default.wav" in my directory and I use a dialog to save it (duplicate it) in another directory with a different name. This is my code: ============ CString strFile; (...) strFile = dlg.GetPathName(); (...) CFile file_orig,file_dest; char *pBuffer; (...) <- part that creates "default.wav" in the same directory file_orig.Open("default.wav", CFile::modeRead); int length = file_orig.GetLength(); // <<<<< here is the problem !! pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); free(pBuffer); ============== Any solution that works can be valid (without including aditional libraries if possible). I just want to keep it as simple as possible. Thanks in advance! :)

      R Offline
      R Offline
      Ravi Bhavnani
      wrote on last edited by
      #2

      ::CopyFile()[^] :) /ravi

      This is your brain on Celcius Home | Music | Articles | Freeware | Trips ravib(at)ravib(dot)com

      1 Reply Last reply
      0
      • S Sousch

        Hello, I'm having a problem when I try to save a file in a different directory (let's say that I simply want to duplicate the file). I have a "default.wav" in my directory and I use a dialog to save it (duplicate it) in another directory with a different name. This is my code: ============ CString strFile; (...) strFile = dlg.GetPathName(); (...) CFile file_orig,file_dest; char *pBuffer; (...) <- part that creates "default.wav" in the same directory file_orig.Open("default.wav", CFile::modeRead); int length = file_orig.GetLength(); // <<<<< here is the problem !! pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); free(pBuffer); ============== Any solution that works can be valid (without including aditional libraries if possible). I just want to keep it as simple as possible. Thanks in advance! :)

        M Offline
        M Offline
        Mark Salsbery
        wrote on last edited by
        #3

        Without knowing the problem you are having, I do believe you should have an access mode AND a sharing mode when using CFile::Open() file_orig.Open("default.wav", CFile::modeRead | CFile::shareDenyWrite); int length = file_orig.GetLength();pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite | CFile::shareExclusive); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); If that doesn't help, maybe try checking the return code :)

        S 1 Reply Last reply
        0
        • M Mark Salsbery

          Without knowing the problem you are having, I do believe you should have an access mode AND a sharing mode when using CFile::Open() file_orig.Open("default.wav", CFile::modeRead | CFile::shareDenyWrite); int length = file_orig.GetLength();pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite | CFile::shareExclusive); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); If that doesn't help, maybe try checking the return code :)

          S Offline
          S Offline
          Sousch
          wrote on last edited by
          #4

          The error consists on an error box saying that it didn't find a file without a name (not exactly in those words because my OS is in spanish language, but that's basically what it says). I've also tried the CopyFile option without success. I have to include "Windows.h" with the headers, include Kernel32.lib in my project settings, and then i would have to put something like this: CopyFile('default.wav',PChar(strFile),TRUE); (am i right?) but it gives "error C2015: too many characters in constant" and error "C2065: 'PChar' : undeclared identifier" Well, i think it it is a little bit too late for me:zzz:. Tomorrow I will keep trying to find a solution.:)

          M 1 Reply Last reply
          0
          • S Sousch

            The error consists on an error box saying that it didn't find a file without a name (not exactly in those words because my OS is in spanish language, but that's basically what it says). I've also tried the CopyFile option without success. I have to include "Windows.h" with the headers, include Kernel32.lib in my project settings, and then i would have to put something like this: CopyFile('default.wav',PChar(strFile),TRUE); (am i right?) but it gives "error C2015: too many characters in constant" and error "C2065: 'PChar' : undeclared identifier" Well, i think it it is a little bit too late for me:zzz:. Tomorrow I will keep trying to find a solution.:)

            M Offline
            M Offline
            Mark Salsbery
            wrote on last edited by
            #5

            I'd say check your pathnames - the source pathname may require the full path with the name unless you set the current directory or it's in the system path. Also, when using CFile::Open, use the 3rd param and you can examine the exception if it fails (returns 0). And you need to do all this before you sleep :laugh:

            1 Reply Last reply
            0
            • S Sousch

              Hello, I'm having a problem when I try to save a file in a different directory (let's say that I simply want to duplicate the file). I have a "default.wav" in my directory and I use a dialog to save it (duplicate it) in another directory with a different name. This is my code: ============ CString strFile; (...) strFile = dlg.GetPathName(); (...) CFile file_orig,file_dest; char *pBuffer; (...) <- part that creates "default.wav" in the same directory file_orig.Open("default.wav", CFile::modeRead); int length = file_orig.GetLength(); // <<<<< here is the problem !! pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); free(pBuffer); ============== Any solution that works can be valid (without including aditional libraries if possible). I just want to keep it as simple as possible. Thanks in advance! :)

              C Offline
              C Offline
              cp9876
              wrote on last edited by
              #6

              I've always used the shell for this:

              SHFILEOPSTRUCT sf;
              sf.hwnd = m\_pMainWnd->m\_hWnd;
              sf.wFunc = FO\_COPY;
              sf.pFrom = pSrc;
              sf.pTo = pDest;	
              sf.fFlags = FOF\_NOERRORUI | FOF\_SILENT | FOF\_NOCONFIRMATION | FOF\_NOCOPYSECURITYATTRIBS; 
              BOOL bSuccess = (SHFileOperation(&sf) == 0);
              
              1 Reply Last reply
              0
              • S Sousch

                Hello, I'm having a problem when I try to save a file in a different directory (let's say that I simply want to duplicate the file). I have a "default.wav" in my directory and I use a dialog to save it (duplicate it) in another directory with a different name. This is my code: ============ CString strFile; (...) strFile = dlg.GetPathName(); (...) CFile file_orig,file_dest; char *pBuffer; (...) <- part that creates "default.wav" in the same directory file_orig.Open("default.wav", CFile::modeRead); int length = file_orig.GetLength(); // <<<<< here is the problem !! pBuffer = (char*)malloc(length); file_orig.Read(pBuffer, length); file_orig.Close(); file_dest.Open( strFile, CFile::modeCreate|CFile:: modeReadWrite); file_dest.Write(pBuffer, file_orig.GetLength()); file_dest.Close(); free(pBuffer); ============== Any solution that works can be valid (without including aditional libraries if possible). I just want to keep it as simple as possible. Thanks in advance! :)

                S Offline
                S Offline
                Sumesh V V
                wrote on last edited by
                #7

                Use can use CopyFile for this purpose

                Known is a drop, unknown is an ocean

                H 1 Reply Last reply
                0
                • S Sumesh V V

                  Use can use CopyFile for this purpose

                  Known is a drop, unknown is an ocean

                  H Offline
                  H Offline
                  Hamid Taebi
                  wrote on last edited by
                  #8

                  Ravi Bhavnani said ;)


                  WhiteSky


                  1 Reply Last reply
                  0
                  Reply
                  • Reply as topic
                  Log in to reply
                  • Oldest to Newest
                  • Newest to Oldest
                  • Most Votes


                  • Login

                  • Don't have an account? Register

                  • Login or register to search.
                  • First post
                    Last post
                  0
                  • Categories
                  • Recent
                  • Tags
                  • Popular
                  • World
                  • Users
                  • Groups