使用 C++ 移动输入文件
Moving the input file with c++
我希望能够在 MoveFile()
中输入任何文件,它会将文件移动到此文件夹:C:\folder\fl.txt
。当我输入 MoveFileA("C:\fl.txt", "C:\folder\fl.txt");
然后一切正常,但我需要将第一个文件(fl.txt
的文件)移动到文件夹 ...
如何实现才能不总是输入文件名 (C:\folder\fl.txt
) 以便它自动输入?
这是我的代码:
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int main() {
MoveFileA("C:\fl.txt", "C:\folder\fl.txt");
cout << "Operation Succesful" << endl;
cout << endl;
system("pause");
}
我需要这样的东西:
int main() {
int path_a;
cin >> path_a;
MoveFileA("path_a", "C:\folder\path_a");
cout << "Operation Succesful" << endl;
cout << endl;
system("pause");
}
也许您正在寻找这样的东西:
#include <iostream>
#include <string>
#include <cstdlib>
#include <Windows.h>
using namespace std;
int main() {
string filename;
cin >> filename;
if (MoveFileA(("C:\"+filename).c_str(), ("C:\folder\"+filename).c_str()))
cout << "Operation Successful" << endl;
else
cout << "Operation Failed" << endl;
cout << endl;
system("pause");
}
不过,如果您使用的是 C++17 或更高版本,您可能会考虑使用 std::filesystem::rename()
而不是 Windows 特定的 MoveFileA()
函数的纯 C++ 解决方案,例如:
#include <iostream>
#include <string>
#include <filesystem>
#include <system_error>
#include <cstdlib>
using namespace std;
namespace fs = std::filesystem;
int main() {
fs::path filename;
cin >> filename;
fs::path root("C:\");
error_code ec;
fs::rename(root / filename, root / "folder" / filename, ec);
if (ec)
cout << "Operation Failed" << endl;
else
cout << "Operation Successful" << endl;
cout << endl;
system("pause");
}
我希望能够在 MoveFile()
中输入任何文件,它会将文件移动到此文件夹:C:\folder\fl.txt
。当我输入 MoveFileA("C:\fl.txt", "C:\folder\fl.txt");
然后一切正常,但我需要将第一个文件(fl.txt
的文件)移动到文件夹 ...
如何实现才能不总是输入文件名 (C:\folder\fl.txt
) 以便它自动输入?
这是我的代码:
#include <iostream>
#include <string>
#include <Windows.h>
#include <stdio.h>
#include <cstdlib>
using namespace std;
int main() {
MoveFileA("C:\fl.txt", "C:\folder\fl.txt");
cout << "Operation Succesful" << endl;
cout << endl;
system("pause");
}
我需要这样的东西:
int main() {
int path_a;
cin >> path_a;
MoveFileA("path_a", "C:\folder\path_a");
cout << "Operation Succesful" << endl;
cout << endl;
system("pause");
}
也许您正在寻找这样的东西:
#include <iostream>
#include <string>
#include <cstdlib>
#include <Windows.h>
using namespace std;
int main() {
string filename;
cin >> filename;
if (MoveFileA(("C:\"+filename).c_str(), ("C:\folder\"+filename).c_str()))
cout << "Operation Successful" << endl;
else
cout << "Operation Failed" << endl;
cout << endl;
system("pause");
}
不过,如果您使用的是 C++17 或更高版本,您可能会考虑使用 std::filesystem::rename()
而不是 Windows 特定的 MoveFileA()
函数的纯 C++ 解决方案,例如:
#include <iostream>
#include <string>
#include <filesystem>
#include <system_error>
#include <cstdlib>
using namespace std;
namespace fs = std::filesystem;
int main() {
fs::path filename;
cin >> filename;
fs::path root("C:\");
error_code ec;
fs::rename(root / filename, root / "folder" / filename, ec);
if (ec)
cout << "Operation Failed" << endl;
else
cout << "Operation Successful" << endl;
cout << endl;
system("pause");
}