连接两个文件时如何避免冗余尾随换行符?

时间:2017-06-29 12:26:18

标签: c++

我想将两个文件合并到一个文件中,当我在合并文件的末尾执行此操作时,会有一个额外的空白行,如何删除它?

file1包含:

1  2  3

和file2包含:

4  5  6

合并后的文件就像

1  2  3
4  5  6
<------cursor is here

这是我的代码:

ifstream read1("1.txt");
ifstream read2("2.txt");

ofstream write ("3.txt");

string line;
string line2;

while ( getline ( read1, line, '\n' ) )
{
 if (!line.empty())
  write << line << endl;
}

while ( getline ( read2, line2, '\n' ) )
{
 if (!line2.empty())
  write << line2 << endl;
}

read1.close();
read2.close();
write.close();

7 个答案:

答案 0 :(得分:1)

我们可以使用streambuf overload for operator<<

#include <fstream>

int main()
{
    std::ifstream read1("1.txt");
    std::ifstream read2("2.txt");
    std::ofstream write("3.txt");
    write << read1.rdbuf() << '\n' << read2.rdbuf();
}

答案 1 :(得分:0)

在第二行读取第一行之前,将其写入文件然后 像你一样使用while,但先输入一个新行。 像这样:

getline(read2 , line2, '\n');
if (!line2.empty()) 
   write << line2;

while ( getline ( read2, line2, '\n' ) )
{
 if (!line2.empty())
  write << endl <<line2;
}

这不是你要求的,但它应该可以解决问题。

答案 2 :(得分:0)

测试输入文件的结尾,不要在最后添加额外的换行符 变化

write << line2 << endl;

{
    write << line2;
    if (!read2.eof()) {write << "\n";}
}

编辑:我添加了大括号来补偿你的单行IF。请尽量避免在您的代码中使用它。看看George如何容易陷入陷阱。

答案 3 :(得分:0)

&LT;&LT; endl将刷新缓冲区并添加一个&#39; \ n&#39; 您可以认为它等同于添加&#39; \ n&#39;最后,为了增加简洁性。

如果您想永远不要添加新行,只需将代码编写为:     ifstream read1(&#34; 1.txt&#34;);     ifstream read2(&#34; 2.txt&#34;);

ifstream read1("1.txt");
ifstream read2("2.txt");

ofstream write ("3.txt");

string line;
string line2;
string buffer
while ( getline ( read1, line, '\n' ) )
{
 if (!line.empty())
  buffer += line;
  buffer += "\n";
}

while ( getline ( read2, line2, '\n' ) )
{
 if (!line2.empty())
        buffer += line2;
        buffer += "\n";
}

//Remove the last char from buffer (the '\n' bothering you)
buffer.substr(0, myString.size()-1)
write << buffer;

read1.close();
read2.close();
write.close();

如果你希望在除了最后一个文件的最后一行之外的每一行之后写一个换行符,而不是将该东西缓冲到一个字符串中并将其写入文件...不是最有效的选项,但是足够好:

ofstream write ("3.txt");

string line;
string line2;

while ( getline ( read1, line, '\n' ) )
{
 if (!line.empty())
  write << line << endl;
}

while ( getline ( read2, line2, '\n' ) )
{
 if (!line2.empty()) {
  write << line2 
 } 
 if(!line2.empty() && !read2.eof() {
  write << line2 << endl;
 } 
}

read1.close();
read2.close();
write.close();

Marek Vitek的答案实际上比我的好:

 <i class="fa fa-chevron-circle-down" aria-hidden="true"></i>

答案 4 :(得分:0)

原始副本怎么样:

std::ifstream read1{path1};
read1.unsetf(std::ios_base::skipws);
std::ifstream read2{path2};
read2.unsetf(std::ios_base::skipws);
std::ofstream write{path3};

std::copy(std::istream_iterator<char>(read1),
          std::istream_iterator<char>(),
          std::ostream_iterator<char> (write));

write << '\n'; // extra EOL between file

std::copy(std::istream_iterator<char>(read2),
          std::istream_iterator<char>(),
          std::ostream_iterator<char> (write));

答案 5 :(得分:-1)

另一个简短的解决方案(虽然我不知道合并结果中最终换行符应该出错):

ifstream read1("1.txt");
ifstream read2("2.txt");

ofstream write ("3.txt");

string line;
string line2;

do {
    if (!line.empty())
       write << line << '\n';
} while( getline ( read1, line, '\n' ) )
write.flush();

do {
    if(!line2.empty())
        write << line2;
} while( getline ( read2, line2, '\n' ) && write << `\n`);

read1.close();
read2.close();
write.close();

答案 6 :(得分:-1)

合并两个有额外换行符的文件

std::ifstream ifs_1( "file1" );
std::ifstream ifs_2( "file2" );

std::ofstream ofs( "file3" );

for( std::string line; std::getline( ifs_1, line ); ){
    if( line != "" ){
        ofs << line << '\n';
    }
}
for( std::string line; std::getline( ifs_2, line ); ){
    if( line != "" ){
        ofs << line << '\n';
    }
}

ifs_1.close();
ifs_2.close();

ofs.close();

测试:

cat file1
one
two
--> empty
--> empty

和file2:

cat file2:
three
four
--> empty
--> empty

输出:

cat file3
one
two
three
four