#include <iostream>
#include "Complex.h"
#include <string>
#include <fstream>
#include <limits>
using namespace std;
static const int bufferLen = 2048;
// 二进制文件的操作
bool CopyFile(const string& src, const string& dest) {// 引用是为了避免创建副本,提高性能
// 打开源文件和目标文件
// 源文件采用二进制读的方式打开
// 目标文件采用二进制写的方式打开
ifstream in(src.c_str(), ios::in | ios::binary);
ofstream out(dest.c_str(), ios::out | ios::binary | ios::trunc);
// 判断文件打开是否成功, 失败返回false
if (!in || !out) {
return false;
}
// 从源文件中读取数据,写到目标文件中
// 通过读取源文件的EOF来判断我们读写的操作是否结束。
char temp[bufferLen];
while (!in.eof()) {
in.read(temp, bufferLen);
streamsize count = in.gcount();
// auto count = in.gcount();
out.write(temp, count);
}
// 关闭我们的源文件和目标文件
in.close();
out.close();
return true;
}
该代码是简单的复制Clion某个工程下的mp3文件拷贝为另一个mp3文件。
但是运行后发现失败:
并且也没看见生成的文件。
在 Clion
中,cpp
源文件经编译后生成可执行文件,放在 cmake-build-debug
目录下,也就是最终的执行目录,所以文件相对路径应该是 ../
或者使用文件的绝对路径也是可以的。
当然,如果相对路径或绝对路径太长也不爽,
可以这样设置:
CLion 在Edit Configurations
, 选择对应的项目, 然后在Working dictionary
里选择相应的程序目录
选择为当前项目就行了。