|
各位表哥们,我想写一个windowsapi的copy的exe小程序,但是无奈小弟才学c++,就是没有什么思路,这是我现在的代码,能够成功的复制。- #include <windows.h>
- #include <tchar.h>
- #pragma comment(lib, "Urlmon.lib")
- int WINAPI _tWinMain(HINSTANCE hInstance, HINSTANCE hPreInstance, LPTSTR IpCmdLine, int nShowCmd)
- {
- BOOL bret = CopyFile(_T("C:\\Users\\Public\\Downloads\\AA.txt"), _T("C:\\Users\\xinxin\\Desktop\\1234.txt"), FALSE);
- if (bret)
- {
- MessageBox(NULL, _T("复制成功"), _T("TIP"), MB_OK);
- }
- else
- {
- MessageBox(NULL, _T("复制失败"), _T("TIP"), MB_OK);
- }
- return 0;
- }
复制代码
我希望后面就是这样运行 copy.exe filename newfilename。
但是我现在却不晓得怎么样用cin 把这2个参数传进去。
希望有表哥能为我解答,可能我的这个问题看起来很蠢很基础,我还是希望有表哥能帮帮我
先谢谢各位表哥们啦!
本帖最后由 cpp2019 于 2021-3-11 18:24 编辑
纯C++实现文件复制
- #include <iostream>
- #include <fstream>
- using namespace std;
- void copy_file(const char* origin, const char* target); //复制文件:origin->target
- int main(int argc, char **argv)
- {
- if (argc != 3)
- {
- cout << "参数错误, 参数一为源文件,参数二为目标文件\n";
- return -1;
- }
- copy_file(argv[1], argv[2]);
- return 0;
- }
- void copy_file(const char* origin, const char* target)
- {
- //读文件内容到缓冲区
- ifstream file_read;
- file_read.open(origin, ios::binary);
- file_read.seekg(0, ios::end);
- unsigned int fileorigin_size = static_cast<unsigned int>(file_read.tellg());
- file_read.seekg(0, ios::beg);
- char* buf = new char[fileorigin_size];
- file_read.read(buf, fileorigin_size);
- file_read.close();
- //缓冲区内容写入新文件
- ofstream file_write;
- file_write.open(target, ios::binary);
- file_write.write(buf, fileorigin_size);
- file_write.close();
- delete[]buf;
- }
复制代码
使用方法同上
- #include <windows.h>
- int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE prevInstance, LPSTR lpCmdLine, int nShowCmd)
- {
- LPWSTR *szArgv = NULL;
- int nArgc = 0;
- //MYCMD cmd = GetCmdLine(lpCmdLine);
- szArgv = CommandLineToArgvW(GetCommandLine(), &nArgc);
- if (nArgc != 3)
- {
- MessageBox(NULL, L"参数错误, 参数一为源文件,参数二为目标文件", L"错误", MB_ICONERROR);
- goto exit;
- }
- if (!CopyFileW(szArgv[1], szArgv[2], FALSE))
- {
- MessageBox(NULL, L"复制失败", L"错误", MB_ICONERROR);
- }
- exit:
- LocalFree(szArgv);
- return 0;
- }
复制代码
|
上一篇: CInternetSession怎么检查当前连接状态下一篇: 学习C++0基础入门第36课,请教拷贝构造函数问题
|