表达式必须具有整数或无作用域枚举类型,并带有字符串向量
expression must have integral or unscoped enum type, with string vectors
所以我正在尝试制作一个程序,该程序可以将某种类型的所有文件从我的下载文件夹移动到它们应该属于的文件夹。
我已经研究了一段时间,终于想出了:
#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;
}
因此,当我在 rename() 中移动文件时,我收到"currentFile"的错误,说"表达式必须具有完整或无作用域的枚举类型"。我假设这是因为你不能像我一样索引,或者类似的东西。
我是C++新手,但有其他编码经验,这对我来说很有意义。
另外,我知道我已经从其他来源获取了代码,但我不打算出售或公开它,它仅供我自己和我个人使用。
您需要更改连接两个字符串的方式,以便:
moveCheck = rename((paths[0] + currentFile).c_str(), (paths[1] + currentFile).c_str());
c_str()
将指针指向每个字符串内的字符缓冲区,因此添加两个指针没有意义。相反,您需要添加两个字符串,然后从串联字符串中获取数据缓冲区。
另一种写法,来自@Martin邦纳和@Nicky
std::string oldPath = paths[0] + currentFile;
std::string newPath = paths[1] + currentFile;
moveCheck = rename(oldPath.c_str(), newPath.c_str());
相关文章:
- 基类类型向量中的派生结构
- 从自定义数据类型向量中删除重复元素
- C++:自定义数据类型向量错误的队列
- C++ - 按自定义数据类型向量的值删除元素
- 在 C++ 中创建类型向量
- 添加字符串向量和不同数据类型向量的映射
- 初始化一个类型向量的巨大向量<int>
- 没有操作员=的类型向量的实例化
- 复杂数据类型向量的迭代器
- 比较两个用户定义的类型向量
- 如何声明二维模板化类类型向量?
- 尝试将用户定义的向量类型插入用户定义的类型向量
- 类类型向量的打印内容
- 如何在 C++ 中将结构类型向量数据输入到结构向量成员(嵌套结构向量)中
- 如何使用新字符串而不是分配器创建自己的字符串类型向量?
- <T> 在使用运算符+ 连接两个向量之前从类型向量中删除索引
- 从自定义数据类型向量数组C 删除
- 如何打印类型向量<元组<字符串、int、int>> 以筛选 c++?
- 正在输出类型向量的成员类型
- 引用类类型向量中的对象成员