2012-01-26 4 views
2

std::string 개체에 경로가 저장된 파일을 삭제하려고합니다. <cstdio>remove()을 알고 있지만 인수로는 const char *이 필요합니다. 문자열 개체를 입력으로 사용하는 함수처럼 파일을 삭제하는 직접적인 방법이 있습니까?C에서 파일 삭제

std::string filename = ... 
remove(filename.c_str()); 

답변

8

c_str() 메서드를 사용할 수 있습니다 :

std::string somePath("/lib/"); 
remove(somePath.c_str()); 
2

std::string 개체가 c_str() 방법을 통해 const char* 표현 당신을 제공 할 것입니다 정의 할 수 있습니다에 대해 어떻게

1

: 물론

string fileName; 
//... 
remove(fileName.c_str()); 

, 당신은 항상

int remove(std::string const& fileName) 
{ 
    return remove(fileName.c_str()); 
} 
2

std::stringstd::stringconst char *을 반환합니다 c_str()라는 메소드가 있습니다. 그걸 이용하십시오!