C++ 在连接两个文件时,如何避免多余的尾随换行符?

C++ 在连接两个文件时,如何避免多余的尾随换行符?,c++,C++,我想将两个文件合并到一个文件中,当我在合并文件的末尾这样做时,会有一个额外的空行,我如何删除它 文件1包含: 1 2 3 4 5 6 文件2包含: 1 2 3 4 5 6 合并的文件如下所示 1 2 3 4 5 6 <------cursor is here 123 4 5 6 在第二行之前读取第一行,将其写入文件,然后 使用while,但要先输入新行。 大概是这样的: getline(read2 , line2, '\n'); if (!line2.

我想将两个文件合并到一个文件中,当我在合并文件的末尾这样做时,会有一个额外的空行,我如何删除它

文件1包含:

1  2  3
4  5  6
文件2包含:

1  2  3
4  5  6
合并的文件如下所示

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

在第二行之前读取第一行,将其写入文件,然后 使用while,但要先输入新行。 大概是这样的:

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

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

为输入文件的结尾编写测试,不要在结尾添加换行符。
改变

write另一个简短的解决方案(尽管我不知道合并结果中的最终换行符会有什么问题):

ifstream read1(“1.txt”);
ifstream read2(“2.txt”);
流写入(“3.txt”);
弦线;
弦线2;
做{
如果(!line.empty())
写入合并两个具有额外换行符的文件
和文件2:

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

cat file3
one
two
three
four
原始副本如何:

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));
std::ifstream read1{path1};
read1.unset(std::ios_base::skipws);
std::ifstream read2{path2};
read2.unset(std::ios_base::skipws);
std::of流写入{path3};
std::copy(std::istream_迭代器(read1),
std::istream_迭代器(),
std::ostream_迭代器(write));
使用“我们能做的事”写入:

#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();
}
#包括
int main()
{
std::ifstream read1(“1.txt”);
std::ifstream read2(“2.txt”);
std::of流写入(“3.txt”);

写入(1)除非您明确需要将内容刷新到磁盘,否则不要使用
endl
。而且您也不需要。使用
write我更喜欢在所有文件的末尾添加换行符(一些代码/程序似乎更好地处理此问题)是否有一个原因,你不希望文件在一个新行上结束?@马立克@ NaNaOLVER——当输入包含一个空行时,你得到一个空字符串。这不是一个读取失败。正式地,C和C++都需要在文本文件的末尾写一个换行符。没有一个意味着读取这样的文件的程序在某些系统上可能失败。,这不是很常见(如果我没记错的话,这是MVS的问题,MVS是一个古老的面向大型机块的文件系统),但通常没有很好的理由来构建不可移植性。@Jarod THX修正了即使在原始文件中存在换行符也会终止换行符的方式。但这可能是OP想要的。他只是想删除额外的空行IMHO。@marek好吧,它完全复制了OPs从实际代码中获得的输出,除了最后一个新代码他们说不需要的行。我同意,他提供的两次输入不会有任何区别。但当2.txt结尾有换行符时,我们的解决方案将给出不同的结果。您的结果将短一个字符。@πάνταῥεῖ 我只有一个循环。你是什么意思?它不仅删除了任何换行符,还增加了额外的换行符newline@k-在我看来,目标似乎是合并两个文件,而不是过滤掉新行。诚然,所讨论的代码确实过滤掉了新行,然后添加回自己的新行作为分隔符,但这似乎是达到一个目的的一种手段,而不是comp完全为OP工作。很明显,输入文件是用换行符分隔的记录,而期望的输出是用换行符分隔的记录。我们只需要文件之间的换行符来分隔相邻的记录,就完成了。我想
if(!line.empty())写@k-five是的,我同意你所提供的代码确实过滤掉了换行符。这个答案是在假设这不是一个要求的情况下工作的,但可能是为了避免在结尾出现换行符。问题可能更清楚,样本数据也不清楚。欢迎OP选择最有效的答案。
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();
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
cat file2:
three
four
--> empty
--> empty
cat file3
one
two
three
four
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));
#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();
}