Best way to duplicate a file?
-
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! :)
-
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! :)
-
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! :)
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 :)
-
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 :)
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.:)
-
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.:)
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:
-
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! :)
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);
-
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! :)
Use can use CopyFile for this purpose
Known is a drop, unknown is an ocean
-
Use can use CopyFile for this purpose
Known is a drop, unknown is an ocean
Ravi Bhavnani said ;)
WhiteSky