首页
社区
课程
招聘
未解决 [求助]C++如何实现删除文本空行的功能
发表于: 2019-6-28 17:26 2625

未解决 [求助]C++如何实现删除文本空行的功能

2019-6-28 17:26
2625
#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个文本文件完全被复制的功能,如何修改代码?

[培训]内核驱动高级班,冲击BAT一流互联网大厂工作,每周日13:00-18:00直播授课

收藏
免费 0
支持
分享
最新回复 (1)
雪    币: 449
活跃值: (233)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
// gggf.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"
#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中不包括每行的换行符
        {
			//line为空就是空行
			if (line!=""){
				cout << line << endl;
                         // 输入到2.txt中
				out << line << endl;
			}else{
				cout<< "{no}" <<endl;
			}
        }
    }
    else // 没有该文件
    {
        cout <<"no such file" << endl;
    }
 
    return 0;
}

最后于 2019-6-28 18:52 被qqsunqiang编辑 ,原因:
2019-6-28 18:51
0
游客
登录 | 注册 方可回帖
返回
//