퍼온 글에는 cout << it->leaf() << endl; 으로 작성되어있지만 boost 1.50 버전에서는 이때 컴파일 버그가 난다
cout << it->path().leaf() << endl; 으로 하면 제대로 나오는것을 알 수 있다
(하지만 정확히 허용되는 버전 범위는 확인하지않음)
The expression itr->path().leaf() == file_name, in the line commented // see below, calls the leaf() function on the path returned by calling the path() function of the directory_entry object pointed to by the iterator. leaf() returns a string which is a copy of the last (closest to the leaf, farthest from the root) file or directory name in the path object.
또한 boost 의
class directory_iterator
: public boost::iterator_facade< directory_iterator,
directory_entry,
boost::single_pass_traversal_tag >
클래스 내용을 보면 directory_iterator 생성자가
directory_iterator(){} // creates the "end" iterator
를 만든다는 내용또한 나와있다 그래서 directory_iterator end; 라고 쓴다
http://cafe.naver.com/javacircle/46010
헤더
<boost/filesystem/path.hpp>, (패스 문자열)
<boost/filesystem/operations.hpp>, (파일 조작)
<boost/filesystem/fstream.hpp>, (파일 입출력)
<boost/filesystem/exception.hpp>, (예외)
<boost/filesystem/convenience.hpp>, (편리 함수 여러가지)
기능
파일·디렉토리 조작
#include <iostream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/fstream.hpp>
using namespace std;
int main()
{
namespace fs = boost::filesystem;
// 디렉토리 작성
fs::path dir( "my_dir" );
fs::create_directory( dir );
// 파일을 열어 기입―
// 디렉토리명과 파일명의 결합은 / 연산자로
fs::ofstream f( dir/"test.txt" );
f << "Hello!" << endl;
f.close();
// 커런트 디렉토리의 파일 일람 해 보자
fs::directory_iterator end;
for( fs::directory_iterator it(fs::current_path()); it!=end; ++it )
{
if( fs::is_directory(*it) )
cout << "D ";
else
cout << "F ";
// leaf() 패스 정보를 자르고, 파일의 이름 부분만 꺼내
cout << it->path().leaf() << endl;
}
return 0;
}
C/C++표준 라이브러리에는, 「파일을 삭제한다」「디렉토리를 작성한다」라고 하는 함수가 존재하지 않습니다. 역사적인 경위는 전혀 모릅니다만, OS마다 디렉토리 단락의 기호가 \ 이거나 : 이거나 / 이거나와 가지각색인 일이나, 원래 디렉토리라고 하는 개념이 없는 환경도 있거나 하기 때문입니다. C++에서는 파일 그 자체를 조작 하려고 하면, CreateDirectory라든가 mkdir나 DeleteFile, unlink, 등의 OS 특유의 기능을 이용할 필요가 있습니다.
이들을 래핑 한 라이브러리( ACE 의 일부나 sord 등)가 등장했습니다. boost::filesystem 도 그 하나입니다.
또,directory_iterator에 의한 디렉토리를 읽거나, 기본적으로 std::fstream 와 같은 fs::fstream 에 의한 파일의 읽고 쓰기도, 라이브러리의 일부로서 포함되어 있습니다.
http://cafe.naver.com/javacircle/12469
디렉터리를 생성, 삭제 또는 파일을 이동하는 작업을 어떠한 개발 환경에서도 동일한 코드로 작성할수 있게 한다는 취지에서 시작한게 바로 boost의 FileSystem 라이브러리 입니다.
#include <iostream>
#include <boost/filesystem/path.hpp>
#include <boost/filesystem/operations.hpp>
using namespace std;
using namespace boost::filesystem;
int main()
{
// 디렉터리 "backup"을 작성
path backup_dir( "backup" );
create_directory( backup_dir );
// "readme.txt"가 존재하면, "backup/readme.bak"에 복사
path work_file( "readme.txt" );
if( exists(work_file) )
copy_file( work_file, backup_dir / "readme.bak" );
// "backup" 디렉터리안의 내용(파일/디렉터리)를 삭제
remove_all( backup_dir );
return 0;
}
http://cobaltic5.tistory.com/63
.생성
- 생성자에 경로를 인자로 넣음, unix, window 방식 모두 지원
. query
- .filename() - 파일명을 리턴함.
- .extension() - 확장자를 리턴함.
- .is_complete() - full path 인지 relative path 인지 알려줌.
. path concatenate
- / operator 이용
- path("c:\\ss\\test"); path2("../test.h");
- path3 = path/path2;
- path3.string() -> c:/ss/test/../test.h 리턴
- path3.normalize() -> c:/ss/test.h 리턴
. link
- libboost-filesystem , libbost-system 필요
. reference
반응형
'메타프로그래밍 > Boost::' 카테고리의 다른 글
boost::mem_fn (0) | 2012.11.13 |
---|---|
boost::unordered_map (0) | 2012.11.13 |
boost::chrono, std::chrono [시간 측정하기] (0) | 2012.11.03 |
Chapter 6. Boost.Container - 번역중 (0) | 2012.10.27 |
Chapter 2. Boost.Any (0) | 2012.10.27 |