2013-04-27 6 views
0

나는 출력 파일이 (다른 확장자) 예는 입력 파일과 동일한 이름을 갖고 싶어 문자열에서 ifstream과 ofstream은 문자열을 허용하지 않습니다. char []를 사용해 보았지만 수정하는 데 어려움이 있습니다.입력/출력 파일 이름

+0

'std :: string'은 문자열 내용으로'char []'를 반환하는'c_str()'메소드를 가지고 있습니다. 그러나 ** c_str()에 의해 반환 된 배열의 내용을 수정해서는 안됩니다 **. –

답변

0

여기가 라이브러리의 성과입니다. 부스트 filesystem library 또는 IDE의 플랫폼 라이브러리 (예 : MFC)를 살펴보십시오.

0

char[]을 사용하고 sprintf으로 수정할 수 있습니다. 다음과 같은 것 :

ofstream outFile_raw; 
ofstream outFile_txt; 
std::string data; 

std::string format_raw="raw"; 
std::string format_txt="txt"; 

char fileName[20]; 
sprintf(fileName, "..\\Packages\\packet_a.%s", format_raw); 

outFile_raw.open(fileName, ios::trunc); 

if(outFile_raw.is_open()) 
{ 
    outFile_raw<< data; // Write contents of the data to the file stream. 
    outFile_raw.close(); 
} 

sprintf(fileName, "..\\Packages\\packet_a.%s", format_txt); 

outFile_txt.open(fileName, ios::trunc); 

if(outFile_txt.is_open()) 
{ 
    outFile_txt<< data; // Write contents of the data to the file stream. 
    outFile_txt.close(); 
}