0

私はプログラムに取り組んでいますが、ディレクトリで発生しているいくつかの問題に遭遇しました。

基本的に、プログラムはユーザーにプログラムのリストを提供するように設計されており、ユーザーがプログラムを選択すると、関連する拡張子を持つすべてのファイルのリストがユーザーに提供されます。ファイルを選択すると、目的のプログラムでファイルが開きます。私の問題は、起動手順とスキャン手順が 2 つの別々のディレクトリで動作しているように見えることです。今のところ、デスクトップにプログラムを含むフォルダーがあるだけです。実行すると、フォルダー内で検索されますが、ファイルを開くと、同じフォルダーのサブフォルダー dir からファイルを開こうとします。問題は検索にあると思います.exeをデスクトップなどの別の場所にコピーすると、ドキュメントに一致する名前のファイルがあった場合、デスクトップからフォルダーが開かれるためです。しかし、それはしません'

これが私が今持っているコードです:

#include <iostream>
#include <filesystem>
#include <vector>
#include <string>
#include <algorithm>
#include <fstream>
#include <Windows.h>
using namespace std;
using namespace std::tr2::sys;


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());
}

void wScan( path f, unsigned i = 0 )
{
directory_iterator d( f );
directory_iterator e;
vector<string>::iterator it2;
std::vector<string> extMatch;

//iterate through all files
for( ; d != e; ++d )
{
    string file = d->path();
    string ext = ".docx";
    //populate vector with matching extensions
    if(ends_with(file, ext))
    {
        extMatch.push_back(file);
    }

}
//counter to appear next to file names
int preI = -1;
//display all matching file names and their counter
for(it2 = extMatch.begin(); it2 != extMatch.end(); it2++)
{
    preI += 1;
    cout << preI << ". " << *it2 << endl;
}
//ask for file selection and assign choice to fSelection
cout << "Enter the number of your choice (or quit): ";
int fSelection;
cin >> fSelection;

cout << extMatch[fSelection] << endl;

//assign the selected file to a string, launch it based on that string and record and output time
    string s = extMatch[fSelection];
    unsigned long long start = ::GetTickCount64();
    system(s.c_str());
    unsigned long long stop = ::GetTickCount64();
    double elapsedTime = (stop-start)/1000.0;
    cout << "Time: " << elapsedTime << " seconds\n";

}


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 (selection == "1")
{
    eScan ( folder );
}
    else if (selection == "2")
{
    vScan ( folder );
}
    else if (selection == "3")
{
    zScan ( folder );
}
    else if (selection == "4")
{
    tScan ( folder );
}
}

あなたが私に提供できるアドバイスを前もって感謝します。

4

1 に答える 1

0

ディレクトリが現在のディレクトリでない場合、ディレクトリ内のファイルにアクセスするには、ファイル名の前にディレクトリへのパスを追加する必要があります。

プログラムがC:/top-level;で実行されているとします。調べたディレクトリがであるとしC:/another-oneます。ディレクトリイテレータが戻ったら、を使用してファイルにアクセスするsomefile.docx必要があります。C:/another-one/somefile.docx

于 2012-11-06T01:37:13.507 に答える