Perl和C/C++比较——文件读取写入,模式匹配
来源:互联网 发布:巴尔扎克与小裁缝 知乎 编辑:程序博客网 时间:2024/05/16 15:25
Perl语言相当的灵活,写起来比较快(我就比较慢了),并且相比之下代码长度也很短。
这是一个曾经写的Perl程序:
#! /usr/bin/perl -w
use strict;
open(F,"/ifs1/PAP/liuyanbo/Cryptorchidism/SNP_result/common_filter") or die"open F error:$!";
open(W,">/ifs1/PAP/liuyanbo/Cryptorchidism/SNP_result/new_filter_again") or die"open W error:$!";
while(my $string=<F>)
{
chomp $string;
my @array=split //s+/,$string;
my $length=scalar@array;
my $B=0;
my $F=0;
for(my $i=3;$i<$length;++$i)
{
if($array[$i]=~/B/){$B=1;}
if($array[$i]=~/F/){$F=1;}
}
if($B==1 and $F==1){print W "$string/n";}
}
close F;close W;
昨天试着用C++写这个程序,弄了好长时间才勉强弄出来:
#include<iostream>
#include<string>
#include<boost/regex.hpp>
#include<fstream>
#include<cstdlib>
#include<vector>
using namespace std;
void StringSplit(string s,char splitchar,vector<string>& vec)
{
if(vec.size()>0)//..vec...
vec.clear();
int length = s.length();
int start=0;
for(int i=0;i<length;i++)
{
if(s[i] == splitchar && i == 0)//.........
{
start += 1;
}
else if(s[i] == splitchar)
{
vec.push_back(s.substr(start,i - start));
start = i+1;
}
else if(i == length-1)//....
{
vec.push_back(s.substr(start,i+1 - start));
}
}
}
int main()
{
const char *Reg_B="B//d+";
boost::regex reg_b(Reg_B);
const char *Reg_F="F//d+";
boost::regex reg_f(Reg_F);
ifstream inFile;
string filename="/ifs1/PAP/liuyanbo/Cryptorchidism/SNP_result/common_filter";//要读取的文件
inFile.open(filename.c_str());
ofstream outFile;
char* outname="/ifs1/PAP/liuyanbo/Cryptorchidism/SNP_result/test_C/data.txt";//写进的文件
outFile.open(outname,ostream::app);
string line;
while(!inFile.eof())
{
getline(inFile,line,'/n');//得到文件的一行
int b_number=0;
int f_number=0;
vector<string> vec;
StringSplit(line,'/t',vec);//按"/t"分割,赋给vec
for(int i=0;i<vec.size();i++)
{
bool b=boost::regex_match(vec[i],reg_b);
if(b){b_number++;}
bool f=boost::regex_match(vec[i],reg_f);
if(f){f_number++;}
}
if(b_number>0&&f_number>0){outFile<<line<<endl;}
}
return 0;
}
这两个程序肯定还可以优化(水平有限),但这里要看的是Perl风格和C风格的差异。
- Perl和C/C++比较——文件读取写入,模式匹配
- 【C#】读取和写入本地txt文件
- C#_File文件读取和写入
- 数据结构 C语言 模式匹配 文件读取
- C文件 写入 读取 函数
- C语言和C++的文件读取、写入函数
- 求大哥大姐,C 链表写入文件和读取
- C语言实现<读取>和<写入> *.ini文件。
- windows下C语言实现<读取>和<写入> *.ini文件。
- c语言创建写入和读取TXT文件数据
- c语言创建写入和读取TXT文件数据
- c语言 读取文件内容 文件写入
- c语言读取文件,写入文件
- C/C++——朴素的模式匹配算法和KMP模式匹配算法
- c#excel文件的读取与写入
- C#xml文件的读取写入
- 标准C++ 读取写入文件代码
- 【C++】文件的读取与写入
- asp conn.execute调用access后台查询
- C#选中模型移动类似于3dmax(附源码)
- mysql查询缓存和执行计划
- [MySQL] InnoDB锁问题
- hadoop summit 2010
- Perl和C/C++比较——文件读取写入,模式匹配
- PHP执行Linux命令的问题
- [MySQL] MyISAM表锁
- OGC标准介绍 12
- 我自豪:我是Zuora的一员
- arm中断控制寄存器详解
- 有关Lucene的问题(3): 向量空间模型与Lucene的打分机制
- java 异常 No bean named 'sessionFactory' is defined
- 常用 UML 符号说明