简体   繁体   中英

expression must have integral or unscoped enum type, with string vectors

So I am trying to make a program that will move all files of a certain type, from my downloads folder to the folder they are supposed to belong.

I have been researching this for awhile and finally have come up with:

#include <iostream>
#include <fstream>
#include <string>
#include <Windows.h>
#include <vector>
#include <stdio.h>

using namespace std;

vector<string> GetFileNamesInDirectory(string directory) {

    vector<string> files;
    HANDLE hFind;
    WIN32_FIND_DATA data;

    hFind = FindFirstFile(directory.c_str(), &data);
    if (hFind != INVALID_HANDLE_VALUE) {
        do {
            files.push_back(data.cFileName);
        } while (FindNextFile(hFind, &data));
        FindClose(hFind);
    }
    return files;
}

int main() {

    string *paths = new string[2];
    string line;
    ifstream pathFile("paths.txt");
    int i = 0;
    vector<string> rsFiles;
    string currentFile;
    int moveCheck;

    if (pathFile.is_open()) {
        while (getline(pathFile, line)) {
            paths[i] = line.substr(line.find_first_of(" ")+1);
            i++;
        }
        pathFile.close();
    }
    else {
        cout << "Unable to open file" << endl;
        return 0;
    }

    rsFiles = GetFileNamesInDirectory(paths[0]+"*.psarc");

    for (int j = 0; j < rsFiles.size(); j++) {

        currentFile = rsFiles[j];

        moveCheck = rename(paths[0].c_str() + currentFile.c_str(), paths[1].c_str() + currentFile.c_str());

    }

    system("pause");
    return 0;
}

So when I go to move the files in rename(), I get an error with 'currentFile' saying "expression must have integral or unscoped enum type". I am assuming this is because you can't index the way that I am, or something along those lines.

I am new to C++, but have other coding experience, and this is what makes sense to me.

Also, I know that I have taken code from other sources, but I do not intend on selling this or making it public, it is only for myself and my own personal use.

You need to change the way you concatenate two strings, to:

moveCheck = rename((paths[0] + currentFile).c_str(), (paths[1] + currentFile).c_str());

c_str() is taking the pointer to the buffer of characters inside each string, so adding two pointers doesn't make sense. Instead, you need to add the two strings, and then grab the data buffer from the concatenated string.


And another way to write this, from @Martin Bonner and @Nicky

std::string oldPath = paths[0] + currentFile; 
std::string newPath = paths[1] + currentFile; 
moveCheck = rename(oldPath.c_str(), newPath.c_str());

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