Capturing files in a directory - Page 2
CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Page 2 of 2 FirstFirst 12
Results 16 to 23 of 23

Thread: Capturing files in a directory

  1. #16
    Join Date
    Mar 2011
    Posts
    144

    Re: Capturing files in a directory

    Ok thanks, is this ok to do? Also, why is it that I'm only calling dir.push_back once, yet I have 2 elements? The first is empty, the 2nd is my push_back call.

    Code:
    			TCHAR SomeArray[_MAX_PATH];
    			buttonPush(hWnd,dir);
    			_tcscpy(SomeArray, dir[1].c_str());
    			SetWindowText(textBox2, SomeArray);
    Code:
    bool buttonPush(HWND hWnd, StringVector &dir ) {
    
    		BROWSEINFO bi;
       TCHAR szDir[MAX_PATH];
       LPITEMIDLIST pidl;
       LPMALLOC pMalloc;
    
       if (SUCCEEDED(SHGetMalloc(&pMalloc)))
       {
          ZeroMemory(&bi,sizeof(bi));
          bi.hwndOwner = hWnd;
          bi.pszDisplayName = 0;
          bi.pidlRoot = 0;
          bi.ulFlags = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT | BIF_USENEWUI | BIF_NONEWFOLDERBUTTON;
    	  bi.pszDisplayName = szDir; // Address of a buffer to receive the display name of the folder selected by the user
    		bi.lpszTitle = _T("Select a folder"); // Title of the dialog
    
          bi.lpfn = BrowseCallbackProc;
    	pidl = SHBrowseForFolder(&bi); 
    		
    		if (pidl) 
    		{ 
    			TCHAR SomeArray[_MAX_PATH];
    			BOOL bRet = ::SHGetPathFromIDList(pidl, SomeArray);
    			dir.push_back(SomeArray);
    			pMalloc->Free(pidl); 
    			pMalloc->Release();
    			if(bRet) return true;
    		} 
        }
       return false;
    }
    Last edited by drwbns; January 10th, 2013 at 01:19 PM.

  2. #17
    Join Date
    Apr 1999
    Posts
    27,423

    Re: Capturing files in a directory

    Quote Originally Posted by drwbns View Post
    why is it that I'm only calling dir.push_back once, yet I have 2 elements?
    What we see in code statically and what the actual flow of the code happens to be are two different things. You have 2 elements because you have 2 elements. That is what you did, and that is what was done. I don't know what else to tell you. Maybe you should debug your code to figure this out. All I see is that "dir" just pops out of nowhere into your code, without any indication where it was declared, where it came from, what accessed it previously, etc.

    Sure I see only one call to push_back, but how do I know if it is done once, twice, or twenty times when the program is run? You don't have a full program posted, so no one knows, how, when, or where these functions are called, or with what data they are being called with.

    Also,
    Code:
    _tcscpy(SomeArray, dir[1].c_str());
    How do you know there is an element at dir[1]? Accessing non-existent elements in an array or vector using [] leads to undefined behaviour.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; January 10th, 2013 at 02:29 PM.

  3. #18
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Capturing files in a directory

    This goes back to your other discussion thread 'TCHAR correct usage'. Why are you using StringVector as a function argument when you are only returning a single name? As per the other thread, why not use string& dir as a function parameter?

  4. #19
    Join Date
    Mar 2011
    Posts
    144

    Re: Capturing files in a directory

    Ah ok, I changed it to a StringClass

  5. #20
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Capturing files in a directory

    A word to the wise. Do you know what your program is trying to do before you start to code it? is this documented? Have you designed it before starting to code? Have you defined AND DOCUMENTED what functions are required and their interfaces? Programming is a lot more than just coding. In your code might I suggest that you insert comments before each function describing what it is supposed to do, what it returns and the purpose of each of its parameters, whether they are [in] only, [out] only or both [in/out], any assumptions made about the value(s) of the [in] parameters, the state of the [out] parameters if an error occurs etc etc etc. Time spent on documentation is time well spent. Others trying to understand your code have an idea of what it should be doing and when you come back to your code in a week/month/year's time and you're forgotten what you did and why then the comments will help - also helps when trying to use a function. The interfaces to a function should be the simplest that is needed to full fill its purpose which should be only one thing.

  6. #21
    Join Date
    Dec 2012
    Location
    England
    Posts
    2,266

    Re: Capturing files in a directory

    You might find it helpful to read 'Problem Solving, Abstraction & Design Using c++' by Friedman and Koffman. Especially sections 1.5, 1.6 and chapter 6 regarding program design. If you can't find a copy in a library, Amazon UK have the 2000 version used for 1.96 (Plus p&p).

  7. #22
    Join Date
    Mar 2011
    Posts
    144

    Re: Capturing files in a directory

    Is there a way to get a const char * from a std::string::iterator when iterating over a std::string?

  8. #23
    VictorN's Avatar
    VictorN is online now Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Wallisellen (ZH), Switzerland
    Posts
    17,281

Page 2 of 2 FirstFirst 12

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  


Azure Activities Information Page

Windows Mobile Development Center


Click Here to Expand Forum to Full Width

This is a CodeGuru survey question.


Featured


HTML5 Development Center