简体   繁体   English

我有2个文本文件,output文本文件和输入文本文件,我怎么append输入文本文件在output文本文件开头的行(c++)?

[英]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;

In the above code, I have used seekg and std::ios::app function but it adds the input text file at the end of output text file.在上面的代码中,我使用seekgstd::ios::app function 但它在 output 文本文件的末尾添加了输入文本文件。 How do I add it at the beginning?如何在开头添加它?

Not tested, but I get:未经测试,但我得到:

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();

For some dumb reason, OS's almost never provide means to prepend a file, I suppose thinking to prevent half-empty sectors;出于某种愚蠢的原因,操作系统几乎从不提供前置文件的方法,我想是为了防止半空扇区; so you must re-write it all.所以你必须全部重写。 Also, you might want to limit the amount of input, so as to not cause a buffer overflow security risk or memory access crash.此外,您可能希望限制输入量,以免造成缓冲区溢出安全风险或 memory 访问崩溃。 Also, block I/O is much faster.此外,块 I/O 更快。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM