【发布时间】:2015-05-10 17:10:18
【问题描述】:
我可以检测到文本中的空行,但不知道如何删除它 请你能给我一些提示如何删除检测到的行
#include <iostream>
#include <fstream>
#include <string>
#include <cstdlib>
using namespace std;
int main()
{
// open input file
std::ifstream ifs( "in_f1.txt" );
std::fstream ofs( "out_f1.txt" );
char c;
char previous_c;
// squeeze whitespace
std::string word;
ifs >> word;
ofs << word;
while (ifs)
{
if (c==' ')
{
ofs.put(c);
while (c==' '&&ifs)
{
ifs.get (c);
;}
}
if (c=='\v')
{
previous_c=c;
while (c=='\v'&&ifs)
{
ifs.get (c);
;}
ofs.put(previous_c);
};
// read line
std::string line;
std::getline( ofs, line );
// append flag and remove 'empty lines'
int flag = 2;
while( getline( ofs, line ) )
{
if( line == " " )
{
flag = 2;
continue;
}
cout << line << " " << flag << endl;
flag = 0;
}
ifs.close();
ofs.close();
}}
【问题讨论】:
-
你也想检测重复行吗?
-
创建一个新文件,将除要删除的文本之外的所有文本写入该新文件,删除原始文件并将新文件重命名为原始文件。
-
你的缩进很糟糕。在访问之前不要设置
c。如果文件的第一行是空白怎么办? -
@Poldie 不只有这些空行,非常感谢你们,