说明:原本是想实现通过在命令行输入 copyfile.exe text.txt new.txt,用C++实现复制文件的功能。但每次运行之后输入指令报错。突然看到
想到可能要把可执行程序的名称是和vs左边列表的名称保持一致才能运行,于是就把copyfile.exe改成了hello.exe。
接着运行直到看到提示:
Suggestion [3,General]: 找不到命令 hello.exe,但它确实存在于当前位置。默认情况下,Windows PowerShell 不会从当前位置加载命令。如果信任此命令,请改为键入“.\hello.exe”。有关详细信息,请参阅 "get-help about_Command_Precedence"。
改动后需要输入 .\hello.exe text.txt new.txt
以下为源代码
#include <iostream>
#include <fstream>
using namespace std;
int main(int argc, char *argv[]) // pointer array
{
if (argc != 3)
{
cerr << "invalid format\n output style : hello.exe name of copied file new file name" << endl;
return 0;
}
ifstream f_read(argv[1], ios::in);
ofstream f_wirte(argv[2], ios::out);
if (!f_read)
{
cerr << "file does not exit!" << endl;
return 0;
}
char i;
while (f_read >> i)
{
f_wirte<<i;
}
cout<<"copied successfully!"<<endl;
f_read.close();
f_wirte.close();
return 0;
}