2017-05-03 5 views
-1

이것은 내 이름으로 저장이 작동하는 방식입니다. 현재 파일의 줄이 첫 번째 그림에 도달 할 때까지 복사 한 다음 인쇄 방법을 사용하여 그림의 정보를 인쇄 한 다음 태그를 닫습니다.기존 텍스트 파일 덮어 쓰기 C++

std::ofstream newFile(filePath1_fixed, std::ios::app); 
std::fstream openedFile(filePath); 
std::string line1; 
while (std::getline(openedFile, line1)) { 
    if (line1.find("<rect") != std::string::npos 
     || line1.find("<circle") != std::string::npos 
     || line1.find("<line") != std::string::npos) 
     break; 
    newFile << line1 << std::endl; 
} 
figc.printToFile(newFile); 
newFile << "</svg>\n"; 

내 질문은 현재 파일의 변경 사항을 저장하는 방법입니다. 나는 이런 식으로 시도했다 :

std::ifstream openedFile(filePath); 
std::ofstream newFile(filePath, std::ios::app); 
std::string line1; 
std::string info_beg[100]; 
int t = 0; 
while (std::getline(openedFile, line1)) { 
    std::cout << "HELLYEAH"; 
    if (line1.find("<rect") != std::string::npos 
     || line1.find("<circle") != std::string::npos 
     || line1.find("<line") != std::string::npos) 
     break; 
    info_beg[t++] = line1; 
} 
for (int i = 0; i < t; i++) 
    newFile << info_beg[i] << std::endl; 
figc.printToFile(newFile); 
newFile << "</svg>\n"; 

이것은 내가 간 가장 가까운 것이다.

<?xml version="1.0" standalone="no"?> 
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg width="12cm" height="4cm" viewBox="0 0 1200 400" 
    xmlns="http://www.w3.org/2000/svg" version="1.1"> 
    <desc>Example rect01 - rectangle with sharp corners</desc> 

    <!-- Show outline of canvas using 'rect' element --> 
    <rect x="1" y="1" width="1198" height="398" 
     fill="none" stroke="blue" stroke-width="2" /> 

    <line x1="20" y1="100" x2="100" y2="20" 
     stroke="red" stroke-width="2" /> 

    <rect x="20" y="30" width="40" height="50" 
     fill="red" stroke="red" stroke-width="1" /> 

    <rect x="10" y="20" width="30" height="40" 
     fill="red" stroke="blue" stroke-width="1" /> 

    <line x1="100" y1="200" x2="300" y2="400" 
     stroke="red" stroke-width="2" /> 

    <circle cx="10" cy="20" r="30" 
     fill="red" stroke="blue" stroke-width="2" /> 

</svg> 
<?xml version="1.0" standalone="no"?> 
<!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" 
    "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"> 
<svg width="12cm" height="4cm" viewBox="0 0 1200 400" 
    xmlns="http://www.w3.org/2000/svg" version="1.1"> 
    <desc>Example rect01 - rectangle with sharp corners</desc> 

    <!-- Show outline of canvas using 'rect' element --> 
    <rect x="1" y="1" width="1198" height="398" 
     fill="none" stroke="blue" stroke-width="2" /> 

    <line x1="20" y1="100" x2="100" y2="20" 
     stroke="red" stroke-width="2" /> 

    <rect x="20" y="30" width="40" height="50" 
     fill="red" stroke="red" stroke-width="1" /> 

    <rect x="10" y="20" width="30" height="40" 
     fill="red" stroke="blue" stroke-width="1" /> 

    <line x1="100" y1="200" x2="300" y2="400" 
     stroke="red" stroke-width="2" /> 

    <circle cx="10" cy="20" r="30" 
     fill="red" stroke="blue" stroke-width="2" /> 

    <rect x="10" y="20" width="30" height="40" 
     fill="red" stroke="blue" stroke-width="2" /> 

</svg> 

그래서 내 실제 질문은 첫 번째를 삭제하거나 덮어 쓰기하는 방법 또는 나는 다른 접근 방식을 필요 :이 얻을. 당신의 std::ofstream의 생성자에서 std::ios::app를 사용

+0

"현재 파일에 변경 사항 저장"을 정의하십시오. 그게 무슨 뜻 이죠? –

+2

더 안전한 방법은 새 파일을 쓰고 (원하는 경우 원본에서 복사) 두 파일을 모두 닫고, 바꾸고, 이전 파일을 삭제하는 것입니다. 2 단계 커밋이라고하며 컴퓨터가 발명 된 이후 안전하게 파일을 수정하는 방법이었습니다. –

+0

생성자의 매개 변수로 "현재 파일"이름과 함께'ofstream'을 사용하면 그것을 덮어 씁니다. –

답변

1

사용 ios::trunc 대신 ios::app

파일에 추가하고 그것을 덮어 쓸 수있는 프로그램을 알려줍니다. 덮어 쓰기 (예 : 잘라 내기)를 원할 경우 std::ios::trunc을 사용하면 기존 파일을 덮어 쓰도록 프로그램에 지시합니다. ofstream은 기본적으로이 작업을 수행하므로 초기화는 단지 std::ofstream newFile(filePath);으로 작성할 수 있습니다.

또한 파일을 읽고 동시에 쓰려고하지 마십시오. 그건 효과가 없을거야. ifstream을 사용하여 버퍼에 데이터를 가져온 다음 close()을 사용하여 파일을 닫습니다. 그런 다음 newFile을 초기화하여 파일을 덮어 쓰고 버퍼를 씁니다.

+0

OP는 동시에 동일한 파일에서 데이터 쓰기를 수행하기 때문에 이보다 더 많은 작업이 필요합니다. –

+0

감사합니다. 도움이되었습니다. 나는 그것을 할 수 있었고, 버퍼를 채운 파일을 닫은 다음 ofstream을 선언해야했습니다. –