簡體   English   中英

在c ++中出現目錄問題。

[英]Issue with directories in c++.

我正在開發一個程序,但遇到了一些問題,這些問題正在發生。

基本上,該程序旨在為用戶提供程序列表,一旦用戶在程序上進行選擇,便向用戶提供所有帶有相關擴展名的文件的列表。 選擇文件后,它將使用所需程序打開文件。 現在我的問題是我的啟動過程和掃描過程似乎在兩個單獨的目錄中運行。 現在,我只是在桌面上有一個包含程序的文件夾。 當它運行時,它會隨文件夾一起搜索,但是當打開文件時,它將嘗試從同一文件夾的子文件夾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 );
}
}

在此先感謝您可以為我提供的任何幫助。

如果該目錄不是當前目錄,則必須在文件名之前添加目錄的路徑,以便訪問目錄中的文件。

假設程序在C:/top-level 假設檢查的目錄是C:/another-one 當目錄迭代器返回somefile.docx ,您需要使用C:/another-one/somefile.docx來訪問文件。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM