2017-05-03 9 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を宣言しなければならなかった。 –

関連する問題