c++ - 在 C++ 中将同一文件加倍间距

标签 c++ file-io

我在 C++ 中编写了一些代码以将文件加倍间距,目前该程序接收一个文件并返回另一个不同的文件,即加倍间距。我希望程序将相同的文件返回到文件目录。在这一点上我很漂亮我需要使用一个临时文件,这很好,但我想在程序结束时最终返回相同的文件(但双倍间距给用户)。任何帮助,将不胜感激。到目前为止,这是我的代码。

#include <fstream>
#include <iostream>
#include <cstdlib>

using namespace std;


int main( )
{
ifstream fin;
ofstream fout;

fin.open("story.txt");
if (fin.fail( ))
{
    cout << "Input file opening failed.\n";
    exit(1);
}

fout.open("numstory.txt");
if (fout.fail( ))
{
    cout << "Output file opening failed.\n";
    exit(1);
}

char next;
int n = 1;
fin.get(next);
fout << n << " ";
while (! fin.eof( ))
{
    fout << next;
    if (next == '\n')
    {
        fout << endl;

    }
    fin.get(next);
}

fin.close( );
fout.close( );

return 0;
}

最佳答案

按照您的建议,创建一个临时文件,然后,仅当处理成功时,删除 现有文件并重命名 临时文件。

顺便说一下,将using namespace std 放在每个程序的顶部是a bad habit你最好避免。

关于c++ - 在 C++ 中将同一文件加倍间距,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24966226/

相关文章:

Java - HtmlUnit - 无法将 HTML 保存到文件(在某些情况下)

c++ - 用数学表达式拆分字符串

c++ - 使用集合作为键映射的机制

c++ - 直接 Access 旧的 MDB 文件

C++ Windows API : disabled menu grayed?

python - enumerate(fileinput.input(file)) 和 enumerate(file) 的区别

c - 写入文件的函数在 .cpp 文件中有效,但在 .h 文件中无效

c++ - 从命令行调用 Eclipse 的格式选项

c++ - 检查类是否等于模板实例或普通类

node.js - 如何通过设置正确的偏移量和位置来读取文件并使用手动缓冲写入 Nodejs 中的响应?