Looking for some help with the next step of my program.
What the program does now, is it asks the user for a which type of file they are looking for. Once the user answers it then searches the folder that the program is in and finds all the files with the extension that matches the requested type. It then lists all of those matching files with a number next to it that iterates with the search.
What I want to be able to do is have the user simply enter the number that corresponds with the file they want to open, and have it open.
What I have now it this:
#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...
}
What I have now is another for loop that goes through the files again and pulls out all the files that match the one requested. It then prints out the names of that file. There is no reason for this, this was just a test to see if my method of searching would find the files I’m looking for.
I would like to know how to go about opening the file once it’s found. I’ve read some stuff on system(), but it seems advised against and it didn’t work for me the way I tried it anyway.
Thanks!
Assuming your code will only be running on Windows (based on the programs listed), you can use the
ShellExecutefunction. To use it, includeWindows.hand pass the program and file you want to open to the function:ShellExecutealso allows you to open a file in the default software associated to the file’s extension. For me, the following also open in Visual C++:ShellExecutecan execute a program with many more options; check out the documentation: http://msdn.microsoft.com/en-us/library/windows/desktop/bb762153%28v=vs.85%29.aspx