除去文本文件每一行的空格&&提取每一行第一个和最后一个字符串

除去文本文件每一行的空格

python:

rstrip()
删除字符串字符串末尾的空格.

lstrip()
截掉字符串左边的空格或指定字符。

strip([chars])
在字符串上执行 lstrip()和 rstrip()

files="conky.conf"
fin =open(files)
fout=open(files+'1','w')

for line in fin:
    print (line)
    fout.write(line.strip()+'\n')

 

 

 

cpp:

#include <ctype.h>      
#include<iostream>      
#include<fstream>      
#include<string.h>      
#define bufsize 300    
using namespace std;      
    
      
int main()      
{      
    char buf[bufsize];      
    ifstream ifs("vimrc.txt",ifstream::in);      
    ofstream ofs("vimrc-.txt",ofstream::out);      
    if (ifs.is_open())      
        cout<<"open file successful"<<endl;      
    else{
        cout<<"open file fail"<<endl;      
        return 1;   
    }
    char *p=NULL;    
    int flag;    
    cout<<"if input 0, only remove space back of line. else remove front and back of line. "<<endl;    
    cin>>flag;    
    while(ifs.getline(buf,bufsize))      
    {      
        int len =strlen(buf);      
        while(isspace(buf[len-1]))      
            len--;      
        buf[len]='\0';      
        if(flag!=0)    
        {    
            len=0;    
            while(isspace(buf[len]))    
                len++;    
            p=buf+len;    
            cout<< p <<endl;    
            ofs<< p <<endl;      
        }    
        else    
        {    
            cout<<buf<<","<<strlen(buf)<<endl;      
            ofs<<buf<<endl;      
        }    
    }      
    ofs.close();       
    ifs.close();      
    return 0;      
}     

提取每一行第一个和最后一个字符串

 

/*************************************************************************
    > File Name: str.cpp
    > Author: ims
    > Created Time: 2017/10/26 18:12:14
 ************************************************************************/

#include<iostream>
#include<fstream>
using namespace std;

int main()
{
	ifstream ifs("3ss.txt",ifstream::in);
    char *p, first[100],buf[200];
    int len=0;
    while(ifs.getline(buf,200))
    {
        int tem=0;
        len=strlen(buf);    
        while(isspace(buf[tem])&&tem<len)    
            tem++;  
        if(tem==len)
            continue;
        else
            p=&buf[tem];
        while(!isspace(buf[tem])&&tem<len)    
            tem++;
        strcpy(first,p);
        first[tem]='\0';
        if(tem==len)
        {
            cout<<"1:only one str:"<<first<<endl;
            continue;
        }
        while(isspace(buf[tem])&&tem<len)    
            tem++;  
        if(tem<len)
        {
            tem=len;
            cout<< "src:"<<buf<<",len:"<< len <<endl;
            while(isspace(buf[tem-1]))      
                tem--;      
            buf[tem]='\0'; 
            while(!isspace(buf[tem-1]))
                tem--;
            p=&buf[tem];
            cout<<"first str:"<< first <<endl;
            cout<<"last str:"<<p<<endl;
        }
        else 
            cout<<"2:only one str:"<<first<<endl;
    }
	return 0;
}

 

 

 

 

 

 

 

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值