Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8774757
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T18:38:54+00:00 2026-06-13T18:38:54+00:00

Looking for some help with the next step of my program. What the program

  • 0

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!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T18:38:55+00:00Added an answer on June 13, 2026 at 6:38 pm

    Assuming your code will only be running on Windows (based on the programs listed), you can use the ShellExecute function. To use it, include Windows.h and pass the program and file you want to open to the function:

    #include <Windows.h>
    
    int main()
    {
        char program[] = "VCExpress.exe";
        char file[] = "main.cpp";
        // opens file in program
        ShellExecuteA(NULL, "open", program, file, NULL, SW_SHOWDEFAULT);
    }
    

    ShellExecute also 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++:

    #include <Windows.h>
    
    int main()
    {
        char file[] = "main.cpp";
        // opens file in default program
        ShellExecuteA(NULL, "open", file, NULL, NULL, SW_SHOWDEFAULT);
    }
    

    ShellExecute can 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

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am looking for some help for a program I am developing. Basically, it
After some help on this website I am now looking for more. This was
I'm a java newbie looking for some help reading a file. Here is the
Looking for some help passing variables from one level to the next in a
Looking for some help! I need to split a string at the last occurrence
looking for some help with images referenced within the stylesheet. I have no problems
Looking for some help with optimising the query below. Seems to be two bottlenecks
I'm a noob and I'm looking for some help on how to run my
-Hello, looking for some help. We currently have a winform app used to do
I need some help. I am looking for a regex that would match the

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.