簡體   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;

在上面的代碼中,我使用seekgstd::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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM