2012-01-26 11 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

を定義することができますについてどのよう

1

:もちろん

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

、あなたは常にstd::stringオブジェクトがc_str()メソッドを介してconst char*表現をご提供します

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

std::stringstd::stringconst char *を返しますc_str()と呼ばれる方法があります。それを利用してください!

関連する問題