私のプログラムの次のステップで助けを探しています。
プログラムが現在行っていることは、ユーザーが探しているファイルの種類を尋ねることです。ユーザーがそれに答えると、プログラムが入っているフォルダーを検索し、要求されたタイプと一致する拡張子を持つすべてのファイルを見つけます。次に、一致するすべてのファイルを一覧表示し、その隣に数字を付けて検索を繰り返します。
私ができるようにしたいのは、ユーザーが開きたいファイルに対応する番号を入力して、それを開かせることです。
私が今持っているものはこれです:
#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
using namespace std::tr2::sys;
    //Checks for matching extensions
bool ends_with(std::string& file, std::string& ext)
{
    return file.size() >= ext.size() && // file must be at least as long as ext
        // check strings are equal starting at the end
        std::equal(ext.rbegin(), ext.rend(), file.rbegin());
}
    //Checks for matching programs
bool program_match(std::string& file, std::string& reqFile)
{
    return std::equal(reqFile.begin(), reqFile.end(), file.begin());
}
void wScan( path f, unsigned i = 0 )
{
    directory_iterator d( f );
    directory_iterator e;
    vector<string>::iterator it2;
    std::vector<string> extMatch;
    std::vector<string> testMatch;
            //loop that populates the vector of matches
    for( ; d != e; ++d )
    {
        string file = d->path();
        string ext = ".docx";
        if(ends_with(file, ext))
        {
            extMatch.push_back(file);
        }
    }
    int preI = -1;
    for(it2 = extMatch.begin(); it2 != extMatch.end(); it2++)
    {
        preI += 1;
        cout << preI << ". " << *it2 << endl;
    }
    cout << "Enter the number of your choice (or quit): ";
    int fSelection;
    cin >> fSelection;
                    //test match for full file match
        for( ; d != e; ++d )
    {
        string file = d->path();
        string reqFile = extMatch[fSelection];
        if(program_match(file, reqFile))
        {
            testMatch.push_back(file);
        }
    }
        for(it2 = extMatch.begin(); it2 != extMatch.end(); it2++)
    {
        cout << *it2 << endl;
    }
}
int main()
{
    string selection;
cout << "0. Microsoft word \n1. Microsoft Excel \n2. Visual Studio 11 \n3. 7Zip \n4. Notepad \n Enter the number of your choice (or quit): ";
cin >> selection;
path folder = "..";
    if (selection == "0")
{
    wScan ( folder );
}
    else if...
}
私が今持っているのは、ファイルを再度調べて、要求されたものと一致するすべてのファイルを引き出す別の for ループです。次に、そのファイルの名前を出力します。これには理由はありません。これは、探しているファイルが私の検索方法で見つかるかどうかを確認するための単なるテストでした。
ファイルが見つかったら開く方法を知りたいです。system() についていくつか読んだことがありますが、それはお勧めできないようで、とにかく私が試した方法ではうまくいきませんでした。
ありがとう!