[英]I have 2 text files , output text file and input text file , how do I append the lines of input text file at the beginning of output text file(c++)?
file.open(input_file);
char z;
while(file.get(z))
{
str1 = str1 + z;
pos1++;
}
file.close();
fout.open(output_file , ios :: app);
file.seekg(0,ios::beg) ;
fout<<endl;
fout<<str1;
fout.close();
file.open(output_file);
file.seekg(0,ios::beg);
char y;
while(file.get(y))
{
cout<<y;
}
file.close();
cout<<endl;
在上面的代码中,我使用seekg
和std::ios::app
function 但它在 output 文本文件的末尾添加了输入文本文件。 如何在开头添加它?
未经测试,但我得到:
file.open(input_file);
char z;
while(file.get(z))
{
str1 = str1 + z;
pos1++;
}
file.close();
fout.open(output_file , ios :: app);
char y;
while(file.get(y))
{
fout<<y;
}
fout<<endl;
fout<<str1;
fout<<endl;
file.close();
出于某种愚蠢的原因,操作系统几乎从不提供前置文件的方法,我想是为了防止半空扇区; 所以你必须全部重写。 此外,您可能希望限制输入量,以免造成缓冲区溢出安全风险或 memory 访问崩溃。 此外,块 I/O 更快。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.