File copy problem

This is a discussion on File copy problem within the Windows Programming forums, part of the Platform Specific Boards category; 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 ...

  1. #1
    Android
    Join Date
    Mar 2004
    Location
    Kurunegala Colony, Sri Lanka, Sri Lanka
    Posts
    375

    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

  2. #2
    Registered User
    Join Date
    Sep 2004
    Location
    California
    Posts
    3,246
    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.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    719
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. A development process
    By Noir in forum C Programming
    Replies: 37
    Last Post: 07-10-2011, 10:39 PM
  2. Subtle(?) File I/O Problem
    By cecomp64 in forum C Programming
    Replies: 9
    Last Post: 07-16-2008, 11:39 AM
  3. sequential file program
    By needhelpbad in forum C Programming
    Replies: 80
    Last Post: 06-08-2008, 01:04 PM
  4. Inventory records
    By jsbeckton in forum C Programming
    Replies: 23
    Last Post: 06-28-2007, 04:14 AM
  5. archive format
    By Nor in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 08-05-2003, 07:01 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21