C++如何实现删除文本空行的功能

[复制链接]
865|1
 楼主| limee 发表于 2019-6-28 17:28 | 显示全部楼层 |阅读模式
#include <fstream>
#include <string>
#include <iostream>
using namespace std;

int main()
{
    ifstream in("hui.txt");
    ofstream out("2.txt");
    string filename;
    string line;

    if(in) // 有该文件
    {
        while (getline (in, line))     // line中不包括每行的换行符
        {
            cout << line << endl;
            out << line << endl;       // 输入到2.txt中
        }
    }
    else // 没有该文件
    {
        cout <<"no such file" << endl;
    }

    return 0;
}

上面的代码是实现2个文本文件完全被复制的功能,如何修改代码?
实现删除空行的功能?
john_lee 发表于 2019-6-29 15:06 | 显示全部楼层
  1. if (line.length() != 0) {
  2.             cout << line << endl;
  3.             out << line << endl;       // 输入到2.txt中
  4. }
您需要登录后才可以回帖 登录 | 注册

本版积分规则

301

主题

475

帖子

0

粉丝
快速回复 在线客服 返回列表 返回顶部