我希望能夠在MoveFile()中輸入任何檔案,它將把該檔案移動到這個檔案夾。C:folderfl.txt。當我輸入MoveFileA("C:fl.txt", "C:folderfl.txt");然后一切都能作業,但我需要將第一個檔案(就是那個fl.txt)移動到檔案夾...
如何實作這一點,以避免總是輸入檔案名(C:folderfl.txt),使其自動輸入呢?
以下是我的代碼:
#include <iostream>
#include <string>
#include <Windows.h>/span>
#include <stdio.h>
#include <cstdlib>/span>
using namespace std;
int main() {
MoveFileA("C:fl.txt", "C:folderfl.txt") 。
cout << "操作成功" << endl。
cout << endl;
system("pause")。
}
我需要這樣的東西:
int main(){
int path_a;
cin >> path_a;
MoveFileA("path_a", "C:folderpath_a") 。
cout << "Operation Succesful" << endl;
cout << endl;
system("pause")。
}
uj5u.com熱心網友回復:
也許你正在尋找類似這樣的東西:
使用 命名空間 std.com.cn>。
int main() {
字串檔案名。
Cin >> 檔案名。
if (MoveFileA(("C:/span> filename). c_str(), ("C:folder/span> filename).c_str())
cout << "操作成功" << endl。
else
cout << "操作失敗" << 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檔案名。
Cin >> filename;
fs::path root("C:/span>)。
error_code ec。
fs::rename(root / filename, root / "folder" / filename, ec) 。
if (ec)
cout << "操作失敗"/span> << endl;
else
cout << "操作成功" << endl;
cout << endl;
system("pause")。
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/320289.html
標籤:
