-
File copy problem
Hi!
I tried to do a simple text file copy like this:
Code:
BROWSEINFO bi;
::ZeroMemory(&bi, sizeof(bi));
TCHAR szSelPath[MAX_PATH];
bi.pszDisplayName = szSelPath;
bi.lpszTitle = "Select the folder/file";
bi.ulFlags =
BIF_BROWSEINCLUDEFILES |
BIF_EDITBOX |
BIF_RETURNFSANCESTORS |
BIF_STATUSTEXT |
BIF_VALIDATE ;
LPITEMIDLIST pidlSrc = ::SHBrowseForFolder(&bi);
char pathSrc[50], pathSrcTemp[50];
SHGetPathFromIDList(pidlSrc,pathSrc);
int j = 0;
for(int i=0; pathSrc[i]!='\0'; i++,j++){
pathSrcTemp[j]=pathSrc[i];
if(pathSrcTemp[j]=='\\'){
j++;
pathSrcTemp[j]='\\';
}
}
pathSrcTemp[j] = '\0';
SHFILEOPSTRUCT shfos;
shfos.hwnd = NULL;
shfos.wFunc = FO_COPY;
shfos.pFrom = pathSrcTemp;
shfos.pTo = "F:\\hi1.txt";
shfos.fFlags = FOF_ALLOWUNDO;
::SHFileOperation(&shfos);
But it isn’t working. I think there is something wrong with string pathSrcTemp. What can I do to fix this, please?
Thanks.
-geek@02
-
You're right, there is something wrong with pathSrcTemp.
Code:
for(int i=0; pathSrc[i]!='\0'; i++,j++){
pathSrcTemp[j]=pathSrc[i];
if(pathSrcTemp[j]=='\\'){
j++;
pathSrcTemp[j]='\\';
}
}
Im not sure what that code is for, but is appears you are just replacing instances of a backslash with a double backslash.
Also, pathSrcTemp needs to end with two null terminators in a row.
-
i didn't really look at your code real good, and i'm not sure what the problem and output is, but consider moving j++;....
also, i may be wrong (if not, you may still be right), but you moight also want to consider using a while loop instead of a for loop... while(pathSrc[i++]){}....if you really want to get creative try while(*pathSrc++){} ...i'm sure someone will warn you against it (don't know why), but nonetheless it's fun to try.