2017-05-03 193 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> 

所以我的实际问题是如何删除第一个或覆盖它或我需要一个不同的方法。

+0

定义“保存对当前文件的更改”。那是什么意思? –

+2

更安全的方法是编写一个新文件(如果您喜欢,请从原始文件复制),然后关闭它们,交换它们并删除旧文件。 这就是所谓的两阶段提交,它是自电脑发明以来安全修改文件的方式。 –

+0

将'ofstream'与“当前文件”名称用作构造函数的参数将覆盖它。 –

回答

1

使用ios::trunc而不是ios::app

在构造函数中使用std::ios::appstd::ofstream告诉程序追加到该文件,而不是覆盖它。如果你想覆盖它(即截断),那么使用std::ios::trunc会告诉程序覆盖现有的文件。 ofstream默认情况下会这样做,所以您可以仅将std::ofstream newFile(filePath);写入初始化。

此外,不要尝试读取文件并同时写入;这将无法正常工作。使用ifstream将数据存入缓冲区,然后使用close()关闭文件。然后初始化newFile以覆盖文件并写出缓冲区。

+0

由于同时从同一个文件获取写入数据,所以OP需要更多。 –

+0

谢谢,这有帮助。我设法做到了,只需关闭填充缓冲区的文件,然后声明thestream。 –