-4

This is belong to visual studio 2013 (v120). How to convert this in visual studio 2010(v100)?

struct {
        bool operator()(const CString &a, const CString &b)
        {
            WIN32_FIND_DATA fa{0},fb{0};
            HANDLE h;

            h = FindFirstFile(a, &fa);
            if (h != INVALID_HANDLE_VALUE) FindClose(h);
            h = FindFirstFile(b, &fb);
            if (h != INVALID_HANDLE_VALUE) FindClose(h);

            return 1 == CompareFileTime(&fa.ftCreationTime, &fb.ftCreationTime);
        }
    } sortproc;

Two error: First : h = FindFirstFile(b, &fb); 13 IntelliSense: identifier "fb" is undefined c:\users\administrator\documents\visual studio 2010\projects\file cleaner\file cleaner\search.h 90 25 File Cleaner

Second: WIN32_FIND_DATA fa{0},fb{0}; 12 IntelliSense: expected a ';' c:\users\administrator\documents\visual studio 2010\projects\file cleaner\file cleaner\search.h 85 21 File Cleaner

andy
  • 35
  • 1
  • 6
  • What is `file` actually? – πάντα ῥεῖ May 05 '15 at 07:16
  • 2
    Your code seems unrelated to your problem (although it's unclear what that problem is) and it's difficult to imagine what storing a `vector` in an array of strings would mean. – molbdnilo May 05 '15 at 07:27
  • I assume you want to iterate over your `std::vector` and store the file names in the `CStringArray`. But what for ? You can get `wchar_t` from both, `CString` and `std::wstring`. – Blacktempel May 05 '15 at 07:30
  • ok i improve the code. – andy May 05 '15 at 07:30
  • But i need to store files in CStringArray. I need to process the files. – andy May 05 '15 at 07:33
  • Why don't you do that then ? `CStringArray::Add(std::wstring::c_str())` – Blacktempel May 05 '15 at 08:14
  • Please sir rewrite the code. Mean implement it? – andy May 05 '15 at 08:22
  • As it's name suggests, `CStringArray` is an array of `CString`s. You can put _filenames_ into an `CStringArray` but you cannot put your `file` type into a `CstringArray`. It's not clear what you actually want. – Jabberwocky May 05 '15 at 08:35
  • Yes i want to put sort files name in CStringArray. And how to do that? – andy May 05 '15 at 08:42
  • CStringArray myarray; ... myarray.Add("MyString"); etc. Read the documentation of CStringArray. – Jabberwocky May 05 '15 at 11:28
  • This seems to be duplicate or continuation of this problem: http://stackoverflow.com/questions/30043803/sort-files-according-to-creation-date You should explain that, it's not reasonable to throw some code and ask people to figure it out. – Barmak Shemirani May 05 '15 at 17:08

1 Answers1

0

You can't sort CStringArray (at least not easily), you have to convert it to vector<wstring> or vector<string> etc.

Use this to convert CStringArray to/from vector<wstring>:

CStringArray sa;
vector<wstring> vws;

vws.resize(0);  //CStringArray to vector<string>
for (int i = 0; i < sa.GetCount(); i++)
    vws.push_back((const wchar_t*)sa[i]);

sa.RemoveAll(); //vector<string> back to CStringArray
for (int i = 0; i < (int)vws.size(); i++)
    sa.Add(vws[i].c_str());

CString has a place in MFC, but CStringArray is not that important, you might do away with it and use vector<CString> instead:

vector<CString> vcs;
//fill vcs...

struct {
    bool operator()(const CString &a, const CString &b)
    {
        WIN32_FIND_DATA fa{ 0 }, fb{ 0 };
        HANDLE h;

        h = FindFirstFile(a, &fa);
        if (h != INVALID_HANDLE_VALUE) FindClose(h);

        h = FindFirstFile(b, &fb);
        if (h != INVALID_HANDLE_VALUE) FindClose(h);

        return 1 == CompareFileTime(&fa.ftCreationTime, &fb.ftCreationTime);
    }
} sortproc;

sort(vcs.begin(), vcs.end(), sortproc);
Barmak Shemirani
  • 30,904
  • 6
  • 40
  • 77