我在 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/