简体   繁体   中英

Issue with directories in c++.

I'm working on a program and I've run into some issues with which directoy things are happening in.

Basically the program is designed to provide the user with a list of programs, once the user selectes on it then provides the user with a list of all of the files with the related extension. Upon selecting a file it will then open the file with the desired program. My problem now is that my launch procedure and my scan procedure seem to operaterating in two seperate directories. Right now I simply have the folder containing the program on my desktop. When it runs it searches withing the folder, but when opening files it tries to open them out of the same folder's subfolder dir. I believe the problem is with the search, because when I copy the .exe to another location, for example the desktop, it will open folders from the desktop, if there happens to be a file with a matching name in my documents. However it won't open if there is no matching file on the desktop.

Here is the code how I have it now:

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

Thanks in advance for any advide you could offer me.

If the directory is not the current directory, you have to prepend the path to the directory before the file name in order to access the file in the directory.

Suppose the program is run in C:/top-level ; suppose the directory examined is C:/another-one . When the directory iterator returns somefile.docx , you need to use C:/another-one/somefile.docx to access the file.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM