C++MFC开发程序,使运行时首先弹出对话框,代码应写哪
前题我是初学C++的,问题是这样的,我在用C++6.0MFC开发一个程序,软件是一个替换文件的功能,我的软件一运行就查找指定文件(如QQ.exe是否在c:\bin\目录下...
前题我是初学C++的,问题是这样的,我在用C++6.0MFC开发一个程序,软件是一个替换文件的功能,我的软件一运行就查找指定文件(如QQ.exe是否在c:\bin\目录下)的路径,如果路径不对,就弹出对话框选择路径,之后在保存路径地址到一个dat文件下,以使下次打开软件时不用在选择路径。
现在的问题是1.我这个代码要写在哪,我找不到主函数,是不是要写在主函数下。
另外一个问题:
1.为什么自动这生成的代码中,比如确定按钮的中文字“确定”的字符定议在哪个文件下了,为什么也找不到。我知道右键属性可以改变,但是我要找他的定议代码,因为我是想在我按下确定之后,按钮变为灰色,不可点,并显示已确定字样。
还有一个问题:怎么MFC给程序界面加上一个图片,或一个网站试的框框,好多软件都有这样的,我是想知道用哪个属性工具加的。
高手回答一下吧,我还正在冲满激情的开发中,希望你们指点一下,谢谢! 展开
现在的问题是1.我这个代码要写在哪,我找不到主函数,是不是要写在主函数下。
另外一个问题:
1.为什么自动这生成的代码中,比如确定按钮的中文字“确定”的字符定议在哪个文件下了,为什么也找不到。我知道右键属性可以改变,但是我要找他的定议代码,因为我是想在我按下确定之后,按钮变为灰色,不可点,并显示已确定字样。
还有一个问题:怎么MFC给程序界面加上一个图片,或一个网站试的框框,好多软件都有这样的,我是想知道用哪个属性工具加的。
高手回答一下吧,我还正在冲满激情的开发中,希望你们指点一下,谢谢! 展开
4个回答
展开全部
C++是门语言,VC6是一门基于C++语言的工具,你需要专门去学一下怎么使用VC6这门工具,这和学C++语言本身是不一样的.
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
建议先看 孙鑫C++视频吧 很有用
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
用 boost的filesystem吧
//FileIO.h
#ifndef __FILE_IO_H__
#define __FILE_IO_H__
#include <fstream>
#include <iostream>
#include <string>
#include "clysedef.hpp"
#include <boost/function.hpp>
#include <vector>
class FileIO
{
public:
FileIO(void);
virtual ~FileIO(void);
static void writeFile(const std::string& fname, const char* buffer, u32 size);
static u8* readFile(const std::string& fname, u32& size);
static bool createDirectory(const std::string& dpathstr, const std::string& dname);
static bool renameDirectory(const std::string& dpath, const std::string& oldname, const std::string newname);
static bool removeDirectory(const std::string& dpathstr, const std::string& dname);
static bool copyFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathstr, const std::string& tagfilestr);
static bool removeFile(const std::string& dpathstr, const std::string& fpathstr);
static bool cutFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathsrc, const std::string& tagfilestr);
static bool renameFile(const std::string& dpathstr, const std::string& srcname, const std::string& tagname);
static void serchfile(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo);
static void serchfileBackFileName(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo);
static void serchfileCountCtrl(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo, u32 count);
static bool isExistFile(const std::string dpathstr, boost::function< bool(const std::string&)> foo);
static bool isExistThisFile(const std::string dpathstr, const std::string& fname);
};
#endif // __FILE_IO_H__
//FileIO.cpp
#include "FileIO.h"
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <algorithm>
#include <numeric>
FileIO::FileIO(void)
{
}
FileIO::~FileIO(void)
{
}
void FileIO::writeFile(const std::string& fname, const char* buffer, u32 size)
{
if(fname.empty())
{
return;
}
try
{
std::ofstream opf;
opf.open(fname.c_str(), std::ios::out | std::ios::binary);
//opf.write((char*)(&size), sizeof(u32));
opf.write(buffer, size);
opf.close();
}
catch(const std::exception& ex)
{
std::cout << ex.what()<<std::endl;
}
};
u8* FileIO::readFile(const std::string& fname, u32& size)
{
if(fname.empty())
{
return 0;
}
u8* buffer = 0;
try
{
boost::filesystem::path fpath(fname.c_str(), boost::filesystem::native);
u32 rsize = boost::filesystem::file_size(fpath);
std::ifstream ipf;
ipf.open(fname.c_str(), std::ios::in | std::ios::binary);
//ipf.read((char*)(&rsize), sizeof(u32));
buffer = new u8[rsize];
ipf.read((char*)buffer, rsize);
ipf.close();
size = rsize;
}
catch(const std::exception& ex)
{
std::cout << ex.what()<<std::endl;
size = 0;
if(buffer)
delete[] buffer;
buffer = 0;
}
return buffer;
};
bool FileIO::createDirectory(const std::string& dpathstr, const std::string& dname)
{
bool bret = true;
std::string full_path = dpathstr;
if((!(full_path.empty())) && (full_path[full_path.size() - 1] != '/'))
{
full_path += "/";
}
full_path += dname;
if(full_path.empty())
{
return true;
}
boost::filesystem::path dpath(full_path.c_str(), boost::filesystem::native);
if(boost::filesystem::exists(dpath))
{
return true;
}
try
{
bret = boost::filesystem::create_directory(dpath);
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
bret = false;
}
full_path.clear();
return bret;
};
bool FileIO::renameDirectory(const std::string& dpath, const std::string& oldname, const std::string newname)
{
bool bret = true;
std::string full_path_old = dpath;
if((!(full_path_old.empty())) && (full_path_old[full_path_old.size() - 1] != '/'))
{
full_path_old += "/";
}
std::string full_path_new = full_path_old;
full_path_old += oldname;
full_path_new += newname;
if(full_path_old.empty() || full_path_new.empty())
{
return false;
}
boost::filesystem::path oldpath(full_path_old.c_str(), boost::filesystem::native);
boost::filesystem::path newpath(full_path_new.c_str(), boost::filesystem::native);
if(!(boost::filesystem::is_directory(oldpath)))
{
return false;
}
try
{
boost::filesystem::rename(oldpath, newpath);
}
catch(const std::exception & ex)
{
std::cout << oldpath.string() << " To " << newpath.string() << " " << ex.what() << std::endl;
bret = false;
}
return bret;
};
bool FileIO::removeDirectory(const std::string& dpathstr, const std::string& dname)
{
bool bret = true;
std::string full_path = dpathstr;
if((!(full_path.empty())) && (full_path[full_path.size() - 1] != '/'))
{
full_path += "/";
}
full_path += dname;
boost::filesystem::path dpath(full_path.c_str(), boost::filesystem::native);
try
{
bret = boost::filesystem::remove(dpath);
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
full_path.clear();
return bret;
};
bool FileIO::copyFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathstr, const std::string& tagfilestr)
{
bool bret = true;
std::string srcfullpath = srcpathstr;
std::string tagfullpath = tagpathstr;
if((!(srcfullpath.empty())) && (srcfullpath[srcfullpath.size() - 1] != '/'))
{
srcfullpath += "/";
}
if((!(tagfullpath.empty())) && (tagfullpath[tagfullpath.size() - 1] != '/'))
{
tagfullpath += "/";
}
srcfullpath += srcfilestr;
tagfullpath += tagfilestr;
boost::filesystem::path srcpath(srcfullpath.c_str(), boost::filesystem::native);
boost::filesystem::path tagpath(tagfullpath.c_str(), boost::filesystem::native);
try
{
boost::filesystem::copy_file(srcpath, tagpath);
}
catch(const std::exception& ex)
{
std::cout << srcpath.string() << " TO " << tagpath.string() << " " << ex.what() << std::endl;
bret = false;
}
return bret;
}
bool FileIO::removeFile(const std::string& dpathstr, const std::string& fpathstr)
{
bool bret = false;
std::string fullpathstr = dpathstr;
if((!(fullpathstr.empty())) && (fullpathstr[fullpathstr.size() - 1] != '/'))
{
fullpathstr += "/";
}
fullpathstr += fpathstr;
boost::filesystem::path fullpath(fullpathstr.c_str(), boost::filesystem::native);
if(boost::filesystem::is_directory(fullpath))
{
return false;
}
try
{
bret = boost::filesystem::remove(fullpath);
}
catch(const std::exception& ex)
{
std::cout << fullpath.string() << " " << ex.what() << std::endl;
}
return bret;
}
bool FileIO::cutFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathsrc, const std::string& tagfilestr)
{
bool bret = false;
try
{
bret |= copyFile(srcpathstr, srcfilestr, tagpathsrc, tagfilestr);
bret |= removeFile(srcpathstr, srcfilestr);
}
catch(const std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
return bret;
}
bool FileIO::renameFile(const std::string& dpathstr, const std::string& srcname, const std::string& tagname)
{
bool bret = true;
std::string full_path_old = dpathstr;
if((!(full_path_old.empty())) && (full_path_old[full_path_old.size() - 1] != '/'))
{
full_path_old += "/";
}
std::string full_path_new = full_path_old;
full_path_old += srcname;
full_path_new += tagname;
if(full_path_old.empty() || full_path_new.empty())
{
return false;
}
try
{
boost::filesystem::path oldpath(full_path_old.c_str(), boost::filesystem::native);
boost::filesystem::path newpath(full_path_new.c_str(), boost::filesystem::native);
if(boost::filesystem::is_directory(oldpath))
{
return false;
}
boost::filesystem::rename(oldpath, newpath);
}
catch(const std::exception & ex)
{
std::cout << ex.what() << std::endl;
bret = false;
}
return bret;
};
void FileIO::serchfile(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()))
{
fnames.push_back(dir_iter->string());
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};
void FileIO::serchfileBackFileName(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()))
{
fnames.push_back(dir_iter->leaf());
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};
void FileIO::serchfileCountCtrl(const std::string& dpathstr, std::vector<std::string>& fnames,
boost::function< bool(const std::string&)> foo, u32 count)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
u32 ncount = 0;
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()) && (ncount < count))
{
fnames.push_back(dir_iter->string());
ncount++;
}
else
{
if(ncount < count)
{
return;
}
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};
bool FileIO::isExistFile(const std::string dpathstr, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()))
{
return true;
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception& ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
return false;
};
bool FileIO::isExistThisFile(const std::string dpathstr, const std::string& fname)
{
std::string srcstr = dpathstr + fname;
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
std::string tstr = (*dir_iter).string();
if(srcstr == tstr)
{
return true;
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception& ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
return false;
};
以上是用BOOST和文件操作
至于如何运行 你就写个对话框之类的程序调用就行了
//FileIO.h
#ifndef __FILE_IO_H__
#define __FILE_IO_H__
#include <fstream>
#include <iostream>
#include <string>
#include "clysedef.hpp"
#include <boost/function.hpp>
#include <vector>
class FileIO
{
public:
FileIO(void);
virtual ~FileIO(void);
static void writeFile(const std::string& fname, const char* buffer, u32 size);
static u8* readFile(const std::string& fname, u32& size);
static bool createDirectory(const std::string& dpathstr, const std::string& dname);
static bool renameDirectory(const std::string& dpath, const std::string& oldname, const std::string newname);
static bool removeDirectory(const std::string& dpathstr, const std::string& dname);
static bool copyFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathstr, const std::string& tagfilestr);
static bool removeFile(const std::string& dpathstr, const std::string& fpathstr);
static bool cutFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathsrc, const std::string& tagfilestr);
static bool renameFile(const std::string& dpathstr, const std::string& srcname, const std::string& tagname);
static void serchfile(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo);
static void serchfileBackFileName(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo);
static void serchfileCountCtrl(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo, u32 count);
static bool isExistFile(const std::string dpathstr, boost::function< bool(const std::string&)> foo);
static bool isExistThisFile(const std::string dpathstr, const std::string& fname);
};
#endif // __FILE_IO_H__
//FileIO.cpp
#include "FileIO.h"
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/path.hpp>
#include <algorithm>
#include <numeric>
FileIO::FileIO(void)
{
}
FileIO::~FileIO(void)
{
}
void FileIO::writeFile(const std::string& fname, const char* buffer, u32 size)
{
if(fname.empty())
{
return;
}
try
{
std::ofstream opf;
opf.open(fname.c_str(), std::ios::out | std::ios::binary);
//opf.write((char*)(&size), sizeof(u32));
opf.write(buffer, size);
opf.close();
}
catch(const std::exception& ex)
{
std::cout << ex.what()<<std::endl;
}
};
u8* FileIO::readFile(const std::string& fname, u32& size)
{
if(fname.empty())
{
return 0;
}
u8* buffer = 0;
try
{
boost::filesystem::path fpath(fname.c_str(), boost::filesystem::native);
u32 rsize = boost::filesystem::file_size(fpath);
std::ifstream ipf;
ipf.open(fname.c_str(), std::ios::in | std::ios::binary);
//ipf.read((char*)(&rsize), sizeof(u32));
buffer = new u8[rsize];
ipf.read((char*)buffer, rsize);
ipf.close();
size = rsize;
}
catch(const std::exception& ex)
{
std::cout << ex.what()<<std::endl;
size = 0;
if(buffer)
delete[] buffer;
buffer = 0;
}
return buffer;
};
bool FileIO::createDirectory(const std::string& dpathstr, const std::string& dname)
{
bool bret = true;
std::string full_path = dpathstr;
if((!(full_path.empty())) && (full_path[full_path.size() - 1] != '/'))
{
full_path += "/";
}
full_path += dname;
if(full_path.empty())
{
return true;
}
boost::filesystem::path dpath(full_path.c_str(), boost::filesystem::native);
if(boost::filesystem::exists(dpath))
{
return true;
}
try
{
bret = boost::filesystem::create_directory(dpath);
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
bret = false;
}
full_path.clear();
return bret;
};
bool FileIO::renameDirectory(const std::string& dpath, const std::string& oldname, const std::string newname)
{
bool bret = true;
std::string full_path_old = dpath;
if((!(full_path_old.empty())) && (full_path_old[full_path_old.size() - 1] != '/'))
{
full_path_old += "/";
}
std::string full_path_new = full_path_old;
full_path_old += oldname;
full_path_new += newname;
if(full_path_old.empty() || full_path_new.empty())
{
return false;
}
boost::filesystem::path oldpath(full_path_old.c_str(), boost::filesystem::native);
boost::filesystem::path newpath(full_path_new.c_str(), boost::filesystem::native);
if(!(boost::filesystem::is_directory(oldpath)))
{
return false;
}
try
{
boost::filesystem::rename(oldpath, newpath);
}
catch(const std::exception & ex)
{
std::cout << oldpath.string() << " To " << newpath.string() << " " << ex.what() << std::endl;
bret = false;
}
return bret;
};
bool FileIO::removeDirectory(const std::string& dpathstr, const std::string& dname)
{
bool bret = true;
std::string full_path = dpathstr;
if((!(full_path.empty())) && (full_path[full_path.size() - 1] != '/'))
{
full_path += "/";
}
full_path += dname;
boost::filesystem::path dpath(full_path.c_str(), boost::filesystem::native);
try
{
bret = boost::filesystem::remove(dpath);
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
full_path.clear();
return bret;
};
bool FileIO::copyFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathstr, const std::string& tagfilestr)
{
bool bret = true;
std::string srcfullpath = srcpathstr;
std::string tagfullpath = tagpathstr;
if((!(srcfullpath.empty())) && (srcfullpath[srcfullpath.size() - 1] != '/'))
{
srcfullpath += "/";
}
if((!(tagfullpath.empty())) && (tagfullpath[tagfullpath.size() - 1] != '/'))
{
tagfullpath += "/";
}
srcfullpath += srcfilestr;
tagfullpath += tagfilestr;
boost::filesystem::path srcpath(srcfullpath.c_str(), boost::filesystem::native);
boost::filesystem::path tagpath(tagfullpath.c_str(), boost::filesystem::native);
try
{
boost::filesystem::copy_file(srcpath, tagpath);
}
catch(const std::exception& ex)
{
std::cout << srcpath.string() << " TO " << tagpath.string() << " " << ex.what() << std::endl;
bret = false;
}
return bret;
}
bool FileIO::removeFile(const std::string& dpathstr, const std::string& fpathstr)
{
bool bret = false;
std::string fullpathstr = dpathstr;
if((!(fullpathstr.empty())) && (fullpathstr[fullpathstr.size() - 1] != '/'))
{
fullpathstr += "/";
}
fullpathstr += fpathstr;
boost::filesystem::path fullpath(fullpathstr.c_str(), boost::filesystem::native);
if(boost::filesystem::is_directory(fullpath))
{
return false;
}
try
{
bret = boost::filesystem::remove(fullpath);
}
catch(const std::exception& ex)
{
std::cout << fullpath.string() << " " << ex.what() << std::endl;
}
return bret;
}
bool FileIO::cutFile(const std::string& srcpathstr, const std::string& srcfilestr, const std::string& tagpathsrc, const std::string& tagfilestr)
{
bool bret = false;
try
{
bret |= copyFile(srcpathstr, srcfilestr, tagpathsrc, tagfilestr);
bret |= removeFile(srcpathstr, srcfilestr);
}
catch(const std::exception& ex)
{
std::cout << ex.what() << std::endl;
}
return bret;
}
bool FileIO::renameFile(const std::string& dpathstr, const std::string& srcname, const std::string& tagname)
{
bool bret = true;
std::string full_path_old = dpathstr;
if((!(full_path_old.empty())) && (full_path_old[full_path_old.size() - 1] != '/'))
{
full_path_old += "/";
}
std::string full_path_new = full_path_old;
full_path_old += srcname;
full_path_new += tagname;
if(full_path_old.empty() || full_path_new.empty())
{
return false;
}
try
{
boost::filesystem::path oldpath(full_path_old.c_str(), boost::filesystem::native);
boost::filesystem::path newpath(full_path_new.c_str(), boost::filesystem::native);
if(boost::filesystem::is_directory(oldpath))
{
return false;
}
boost::filesystem::rename(oldpath, newpath);
}
catch(const std::exception & ex)
{
std::cout << ex.what() << std::endl;
bret = false;
}
return bret;
};
void FileIO::serchfile(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()))
{
fnames.push_back(dir_iter->string());
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};
void FileIO::serchfileBackFileName(const std::string& dpathstr, std::vector<std::string>& fnames, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()))
{
fnames.push_back(dir_iter->leaf());
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};
void FileIO::serchfileCountCtrl(const std::string& dpathstr, std::vector<std::string>& fnames,
boost::function< bool(const std::string&)> foo, u32 count)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
u32 ncount = 0;
try
{
if(boost::filesystem::is_directory(dpath))
{
//std::cout << dpath.string() << "\n";
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()) && (ncount < count))
{
fnames.push_back(dir_iter->string());
ncount++;
}
else
{
if(ncount < count)
{
return;
}
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception & ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
};
bool FileIO::isExistFile(const std::string dpathstr, boost::function< bool(const std::string&)> foo)
{
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
if(foo(dir_iter->leaf()))
{
return true;
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception& ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
return false;
};
bool FileIO::isExistThisFile(const std::string dpathstr, const std::string& fname)
{
std::string srcstr = dpathstr + fname;
boost::filesystem::path dpath(dpathstr.c_str(), boost::filesystem::native);
try
{
if(boost::filesystem::is_directory(dpath))
{
boost::filesystem::directory_iterator end_iter;
for ( boost::filesystem::directory_iterator dir_iter( dpath ); dir_iter != end_iter; ++dir_iter )
{
try
{
if(boost::filesystem::is_directory(*dir_iter))
{
continue;
}
std::string tstr = (*dir_iter).string();
if(srcstr == tstr)
{
return true;
}
}
catch(const std::exception & ex)
{
std::cout << dir_iter->string() << " " << ex.what() << std::endl;
}
}
}
}
catch(const std::exception& ex)
{
std::cout << dpath.string() << " " << ex.what() << std::endl;
}
return false;
};
以上是用BOOST和文件操作
至于如何运行 你就写个对话框之类的程序调用就行了
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询