【问题标题】:Overwrite an existing text file c++覆盖现有的文本文件 c++
【发布时间】:2017-05-03 17:22:16
【问题描述】:

这就是我的“另存为”的工作方式——它复制当前文件的行,直到它到达第一个图形,然后我使用我的打印方法打印图形的信息,然后关闭标签。

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>

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

【问题讨论】:

  • 定义“将更改保存到当前文件”。这是什么意思?
  • 一种更安全的方法是编写一个新文件(如果您愿意,可以从原始文件复制),然后关闭它们,交换它们,然后删除旧文件。它被称为两阶段提交,自计算机发明以来,它一直是安全修改文件的方式。
  • 使用带有“当前文件”名称的ofstream作为构造函数的参数将覆盖它。
  • 如果您想覆盖现有内容,您能解释一下为什么选择使用std::ios::app 吗?做出这个决定的思考过程是什么?
  • @GeorgiGenchev:好吧,靠猜测编程是行不通的。根据其定义,显然std::ios::app 是错误的。所以这不是一个解决方案。解决方案是不要尝试同时读取和写入同一个文件!

标签: c++ file text fstream ofstream


【解决方案1】:

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

std::ofstream 的构造函数中使用std::ios::app 告诉程序附加到文件而不是覆盖它。如果你想覆盖它(即截断),那么使用std::ios::trunc 将告诉程序覆盖现有文件。 ofstream 默认执行此操作,因此您可以将初始化编写为 std::ofstream newFile(filePath);

另外,不要尝试同时读取和写入文件;那是行不通的。使用ifstream 将数据放入缓冲区,然后使用close() 关闭文件。然后初始化newFile覆盖文件并写出缓冲区。

【讨论】:

  • OP 需要的不仅仅是这个,因为要同时从同一个文件中获取要写入的数据。
  • 谢谢,这有帮助。我设法做到了,只需要关闭填充缓冲区的文件,然后声明 ofstream。
猜你喜欢
  • 1970-01-01
  • 2015-09-14
  • 2016-05-25
  • 2016-04-22
  • 2014-01-10
  • 1970-01-01
  • 2013-11-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多