내 프로그램의 다음 단계에 대한 도움을 얻으려고합니다.문자열과 일치하는 프로그램 찾기 및 실행
프로그램이 지금하는 일은 사용자가 찾고있는 파일 유형을 묻는 것입니다. 사용자가 대답하면 프로그램이있는 폴더를 검색하고 요청한 유형과 일치하는 확장명을 가진 모든 파일을 찾습니다. 그런 다음 검색과 반복되는 번호 옆에 일치하는 모든 파일을 나열합니다.
내가 원하는 것은 사용자가 열어보고 싶은 파일에 해당하는 번호를 입력하고 열어 보도록하는 것입니다.
지금이 무슨이 :
#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...
}
내가 지금 가지고있는 것은 다시 파일을 통해 이동하고 요청 된 하나와 일치하는 모든 파일을 꺼내서 그 루프에 대한 또 다른입니다. 그런 다음 해당 파일의 이름을 인쇄합니다. 이것에 대한 이유는 없습니다, 이것은 검색 방법이 제가 찾는 파일을 찾을 수 있는지를보기위한 테스트 일뿐입니다.
일단 발견되면 파일을 여는 방법을 알고 싶습니다. 나는 시스템()에서 몇 가지 물건을 읽었지만 반대 의견을 제시하고 그것이 어쨌든 시도한 방식으로 나에게 도움이되지 못했다.
감사합니다.
죄송합니다. 죄송 합니다만, 나는 링크를 알아 차리지 못했고, 텍스트로 읽었습니다. 고맙습니다! – Sh0
특정 프로그램 (예 : Excel)에서 파일을 열려면 올바른 매개 변수로 Windows API에서 ShellExecute 함수를 호출 할 수 있습니다. 이 함수에 대한 문서는 다음과 같습니다. http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx 윈도우의 특정 기능이므로 다른 곳으로 포팅해야합니다. 시스템. – Ryan
어쩌면 나는 오해하고있다. 그러나 그것은 모두 C++ 내에서 읽기 용 파일을 여는 것에 관한 것 같다. 내가 찾고있는 것은 파일과 관련된 프로그램을 시작하는 방법이다. 예를 들어, test.docx를 선택하면 Microsoft Word에서 test.docx가 열립니다. – Sh0