C++技巧(C++文件拷贝代码)

C++技巧(C++文件拷贝代码),第1张

C++技巧(C++文件拷贝代码),第2张

C++中的一些文件复制程序远比我想象的要难,直接操作windows api比C#中只用的File类和Directory类更有趣。这个过程让我体会到的不仅仅是api,还有编程的本质。
我先把我的代码分享给大家,请各位C++前辈指教。
。h file
# pragma once
# include
# include " file . h "
使用命名空间STD
命名空间公共
{
命名空间文件
{
类sync folder:public Common::File::File class
{
private:
wstring m _ source directory;
wstring m _ target directory;
const wstring * m _ ext;
const bool * m _ reverse ext;
protected:
void file method(const wstring &);
public:
sync folder(const wstring & source directory,const wstring &targetDirectory,const wstring &ext,bool reverseExt):FileClass(),
m _ source directory(Common::File::GetSecurePath(source directory))、m _ target directory(Common::File::GetSecurePath(target directory))
{
m _ ext = new wstring(ext);
m _ reverse ext = new bool(reverse ext);
}
~ sync folder(void)
{
delete m _ ext;
删除m _ reverseExt
}
void Exec();
};
}
}
。cpp文件
# include " STD afx . h "
# include " sync folder . h "
# include " File . h "
void Common::File::sync folder::Exec()
{
read files(m _ source directory,* m _ ext);
}
void Common::File::sync folder::File method(const wstring & fileName)
{
/Get相对路径
wstring offset path =
Common::File::GetRelativePath(m _ source directory,
Common::File::GetPathFromFilePath(fileName));
//获取新路径
wstring target file path;
if(offset path . empty())
target file path = m _ target directory;
else
target file path = m _ target directory+L " \ "+offset path;
//创建文件夹
common:: file:: create path(目标文件路径);
//新建文件路径
wstring new filename = target file path+l " \ "+common::file::getfilename from file path(filename);
//复制文件
copyfile (filename.c _ str(),newfilename.c _ str(),true);
}
遍历文件目录的函数(文件夹遍历代码C++(win32平台))。在这个函数的参数列表中,有一个函数指针。考试。重要的提示是,通过这个函数指针,您可以处理遍历的文件。因为在C#中可以通过delegate声明一个类似函数指针的类型,然后通过这个类型定义一个对象指向执行方法的地址,所以我想在C++中也这么做。但是最后C++编译器告诉我,这个写法不对。在C++中,函数指针指向静态地址。如果我想把指针指向一个类实例的成员方法,这在C++中是不允许的(我在boost中听到了一个解决方案)。考试。大提示C++和C#的相似之处并不相同。试想C#编译器是如何使用delegate实现函数指针的功能的,或者说两者有本质的区别。
另一个重要的体验是,C++的开发环境远不如C# (VS2008)智能,尤其是智能感知。这总让我在一些代码的开头感觉不稳定,走路像个醉汉,摇摇晃晃的。后来我发现,这种担心源于我自己对C++编译器的类型检查特性缺乏充分的了解。我们可以放心大胆地写代码,然后编译。如果有问题,编译器会准确地告诉你。这些知识对重构代码非常有帮助。在接下来的工作中,我觉得重构那些C#代码很舒服,因为编译器会告诉我问题出在哪里。也就是基于这样的认识,在编写C#代码的时候,尽量避免使用datatable这样的类型,因为编译器无法对其中的数据类型进行类型检查,这样我们就享受不到编译器类型检查的好处了。
以及单元测试是如何工作的。我开始写C++代码,参加考试。大提示我每次写一行代码,都不确定输出结果是否正确,也不确定是什么样子。为了解决这个问题,我基本上是为每个函数编写专门的调试代码。后来发现这是一种很好的工作方式,效率高。这种工作方式就是。网络世界。

位律师回复
DABAN RP主题是一个优秀的主题,极致后台体验,无插件,集成会员系统
白度搜_经验知识百科全书 » C++技巧(C++文件拷贝代码)

0条评论

发表评论

提供最优质的资源集合

立即查看 了解详情