回复 20楼 pangding
谢谢你,pangding版主:)可以请教你,若是用c++语言,应该如何写此程序呢?
我试着把档案该小过1GB,再做比较...
有些简单的计算,awk或许会蛮快...
但若是复杂些的计算,可能就要比较耗时 :(
我准备多些档案后,迟点再上载来,供大家讨论和分享 :)
// aa.cpp : Defines the entry point for the console application. // #include "stdafx.h" #include <stdio.h> #include <string.h> int main(int argc, char* argv[]) { FILE *pOpen; char chBuf[200]; int nSum=0; long nLen=0; int k; pOpen = fopen("d:\\aa.txt","r"); //打开文件 if (pOpen==NULL) { printf("打开文件失败"); } else { //打开文件成功,读取内容 fread(chBuf,200,1,pOpen); //开始统计 nLen = strlen(chBuf); for(int k=0;k < nLen;k++) { if(chBuf[k]=='A' || chBuf[k]=='C' || chBuf[k]=='G' || chBuf[k]=='T' || chBuf[k]=='N') { nSum++; } } fclose(pOpen); } printf("%d\n",nSum); return 0; }
c++程序名称 读取档案名称 结果档案名称 eg. a.out input_file.txt output_file.txt a.out input_file2.txt output_file2.txt
int main(int argc,char **argv) { FILE *pf=fopen(argv[1],"rb+"); fseek(pf,0,SEEK_END); int filesize=ftell(pf); fseek(pf,0,SEEK_SET); int num=1024*1024; char *pbFile=new char[num+1]; bool bFlag=false; int target[26]={0}; if(pbFile==NULL) { printf("new error"); return -1; } while(filesize>0) { if(filesize<num) num=filesize; fread(pbFile,num,1,pf); for(int i=0;i<num;i++) { while((i<num && pbFile[i]=='>') || bFlag) { bFlag=true; while(i<num && pbFile[i]!='\n')i++; if(i<num) { bFlag=false; i++; } else if(i==num) break; } while(i<num && pbFile[i]!='\r' && pbFile[i]!='\n') target[pbFile[i++]-65]++; } filesize-=num; } delete []pbFile; fclose(pf); FILE *p=fopen(argv[2],"r+"); fprintf(p,"%d",target[0]+target['G'-'A']+target['C'-'A']+target['T'-'A']); fclose(p); cout<<target[0]+target['G'-'A']+target['C'-'A']+target['T'-'A']<<endl; return 0; }
#include <stdio.h> #include <stdlib.h> int main(int argc,char **argv) { FILE *pf=fopen(argv[1],"rb+"); fseek(pf,0,SEEK_END); int filesize=ftell(pf); fseek(pf,0,SEEK_SET); . .