二进制的文件读写 rb与 rb+
rb:为输入打开一个二进制文件;
rb+ 指为读写打开一个二进制文件;
rb为只读,对于不需要进行更新的文件,可以防止用户的错误的写回操作,防止损毁原有数据。具有较高的安全性~~~
rb+为更新二进制文件,可以读取,同时也可以写入,需要用到fseek之类的函数进行配合,以免出错~~~
FILE* fp = nullptr; const char* fname = "../test.h264"; fp = fopen(fname, "rb+");
if(!fp)
{
std::cout<<"打开文件失败!"<<endl;
}
#ifndef COPYDIR_H
#define COPYDIR_H
#include <string>
#include <vector>
class CopyDir
{
public:
CopyDir();
~CopyDir();
void copy(const std::string& srcDirPath, const std::string& desDirPath);
public:
private:
bool make_dir(const std::string& pathName);
//bool mkdir (char const* pathname/*, mode_t mode*/);
bool get_src_files_name(std::vector<std::string>& fileNameList);
void do_copy(const std::vector<std::string>& fileNameList);
private:
std::string srcDirPath, desDirPath;
};
#endif // COPYDIR_H
#include "copy_dir.h"
#include <iostream>
#include <fstream>
#include <cstring>
#pragma warning(disable: 4996)
#if defined(_WIN32)
# include <direct.h>
# include <io.h>
# include <shlobj.h>
# include <sys/stat.h>
# include <sys/types.h>
#else // Linux
# include <dirent.h>
# include <unistd.h>
# include <sys/stat.h>
# include <sys/types.h>
# include <pwd.h>
#endif
CopyDir::CopyDir()
{
}
CopyDir::~CopyDir()
{
}
void CopyDir::copy(const std::string& srcDirPath, const std::string& desDirPath)
{
this->srcDirPath = srcDirPath;
std::string srcDir;
#ifdef _WIN32
int n = 0;
while (srcDirPath.find('\', n) != std::string::npos)
{
n = srcDirPath.find('\', n) + 1;
}
if (n == 0)
{
std::cout << "src path error" << std::endl;
return;
}
srcDir = srcDirPath.substr(n - 1, srcDirPath.size());
#else // Linux
int n = 0;
while (srcDirPath.find('/', n) != std::string::npos)
{
n = srcDirPath.find('/', n) + 1;
}
if (n == 0)
{
std::cout << "src path error" << std::endl;
return;
}
srcDir = srcDirPath.substr(n - 1, srcDirPath.size());
#endif
this->desDirPath = desDirPath + srcDir;
if (!make_dir(this->desDirPath))
{
return;
}
std::vector<std::string> fileNameList;
if (!get_src_files_name(fileNameList))
{
return;
}
if (fileNameList.empty())
{
std::cout << "src dir is empty" << std::endl;
return;
}
do_copy(fileNameList);
}
bool CopyDir::make_dir(const std::string& pathName)
{
if (access(pathName.c_str(), 0) == -1) //文件夹不存在则生成一个
{
std::cout << pathName << " is not existing" << std::endl;
std::cout << "now make it" << std::endl;
#ifdef _WIN32
if (::_mkdir(pathName.c_str()) < 0)
{
std::cout << "create path error" << std::endl;
return false;
}
#else // Linux
if (::mkdir(pathName.c_str(), S_IRWXU | S_IRGRP | S_IXGRP) < 0)
{
std::cout << "create path error" << std::endl;
return false;
}
#endif
}
return true;
}
bool CopyDir::get_src_files_name(std::vector<std::string>& fileNameList)
{
#ifdef _WIN32
_finddata_t file;
long lf;
std::string src = this->srcDirPath + "\*.*";
if ((lf = _findfirst(src.c_str(), &file)) == -1)
{
std::cout << this->srcDirPath << " not found" << std::endl;
return false;
}
else{
while (_findnext(lf, &file) == 0)
{
if (strcmp(file.name, ".") == 0 || strcmp(file.name, "..") == 0)
continue;
fileNameList.push_back(file.name);
}
}
_findclose(lf);
#else // Linux
DIR *dir;
struct dirent *ptr;
if ((dir = opendir(this->srcDirPath.c_str())) == NULL)
{
std::cout << this->srcDirPath << " not found" << std::endl;
return false;
}
while ((ptr = readdir(dir)) != NULL)
{
if ((ptr->d_name == ".") || (ptr->d_name == "..")) //current / parent
continue;
else if (ptr->d_type == 8) //file
fileNameList.push_back(ptr->d_name);
else if (ptr->d_type == 10) //link file
continue;
else if (ptr->d_type == 4) //dir
fileNameList.push_back(ptr->d_name);
}
closedir(dir);
#endif
return true;
}
void CopyDir::do_copy(const std::vector<std::string> &fileNameList)
{
#pragma omp parallel for
for (int i = 0; i < fileNameList.size(); i++)
{
std::string nowSrcFilePath, nowDesFilePath;
#ifdef _WIN32
nowSrcFilePath = this->srcDirPath + "\" + fileNameList.at(i);
nowDesFilePath = this->desDirPath + "\" + fileNameList.at(i);
#else
nowSrcFilePath = this->srcDirPath + "/" + fileNameList.at(i);
nowDesFilePath = this->desDirPath + "/" + fileNameList.at(i);
#endif
std::ifstream in;
in.open(nowSrcFilePath, std::ios::binary);
if (!in)
{
std::cout << "open src file : " << nowSrcFilePath << " failed" << std::endl;
continue;
}
std::ofstream out;
out.open(nowDesFilePath, std::ios::binary);
if (!out)
{
std::cout << "create new file : " << nowDesFilePath << " failed" << std::endl;
in.close();
continue;
}
out << in.rdbuf();
out.close();
in.close();
}
}
#ifdef _WIN32 std::string src = "D:\a"; std::string des = "C:"; CopyDir cd; cd.copy(src, des); #else std::string src = "/media/myUbuntu/F/data/test"; std::string des = "/media/myUbuntu/F/data/test2"; CopyDir cd; cd.copy(src, des); #endif