如何附加到一个文件,然后将所述文件复制到另一个文件中
How to append to one file, then copy said file into another file
我觉得我什么都试过了,我可以把第一个文件附加到第二个文件,但不能把第二个附加到第三个文件。我做错了什么?
为了清楚起见,我需要取一个文件,将其附加到第二个文件中,然后将第二个文档的内容放入第三个文档中。我可以通过将两个文件都放入字符串,然后将这些字符串放入第三个文件来模拟这种结果,但这在这个问题中是不正确的。
我对任何方式或技术都不挑剔,我试过一些,但都不管用。这是最新的尝试,对于最后一步仍然不起作用。
这是我的代码:
#include <iostream>
#include <string>
#include <fstream>
using namespace std;
int main()
{
string a,b,c;
cout << "Enter 3 file names: ";
cin >> a >> b >> c;
fstream inf;
ifstream two;
fstream outf;
string content = "";
string line = "";
int i;
string ch;
inf.open(a, ios::in | ios:: out | ios::app);
two.open(b);
outf.open(c, ios::in);
//check for errors
if (!inf)
{
cerr << "Error opening file" << endl;
exit(1);
}
if (!two)
{
cerr << "Error opening file" << endl;
exit(1);
}
if (!outf)
{
cerr << "Error opening file" << endl;
exit(1);
}
for(i=0; two.eof() != true; i++)
content += two.get();
i--;
content.erase(content.end()-1);
two.close();
inf << content;
inf.clear();
inf.swap(outf);
outf.close();
inf.close();
return 0;
这里有一个想法:
#include <fstream>
using namespace std;
void appendf( const char* d, const char* s )
{
ofstream os( d, ios::app );
if ( ! os )
throw "could not open destination";
ifstream is( s );
if ( ! is )
throw "could not open source";
os << is.rdbuf();
}
int main()
{
try
{
appendf( "out.txt", "1.txt" );
return 0;
}
catch ( const char* x )
{
cout << x;
return -1;
}
}
相关文章:
- C++-试图将函数指针推回到另一个CPP文件中的矢量时出错
- 输入到文件并输出到另一个文件,并将流文件传递给函数
- C++试图读取一个文件并输出到另一个文本文件
- 如何从另一个文件继承私有成员变量和公共函数
- 在 Windows 上,是否可以让 dll 在不使用 PATH 环境变量的情况下在另一个文件夹中查找依赖项?
- C++ 如何打开带有另一个目录的输入文件?
- 如何在 Gnuplot 中分别绘制 2 个文件数据?我有一个文件"sin.txt",另一个文件"cos.txt",我想将它们分别绘制在一个图表上
- 从另一个 cpp 文件更改结构内、映射键内的变量
- 程序在使用 system() 启动另一个可执行文件时停止
- 如何在安卓工作室中将c ++头文件从一个文件夹导入到另一个文件夹?
- 如何在另一个 CPP 文件中使用一个 cpp 文件中的函数?
- 使用 scope 运算符 (::) 引用另一个文件中的类
- JNI,使用两个 .so 文件时出错,其中一个文件需要另一个文件
- 如何将包含另一个对象向量的对象保存到文件中,并使用C++中的二进制文件从文件中读回?
- 两个线程一个使用流 Api,另一个线程创建文件失败并出现错误ERROR_SHARING_VIOLATION
- 为什么我不能从同一目录中的另一个文件中 #include 我的类?
- 使用命名空间在另一个".cpp"文件中定义
- 在 c++ 中打开文件、删除标点符号并追加到另一个文件中
- C++ 如何将两个 makefile 对象目标规则(位于另一个文件夹中)合并到一个目标/规则中?
- 是否可以将子进程的 stdout 重定向到父进程中的另一个文件?