一開始用fopen 讀取文件的時候 老是返回Null python
Can't open (null) for reading. 緣由是最後一個參數後面有「\n」ide
對於fasta序列有多行的狀況,須要把字符串鏈接, 這在perl 或 python裏面都比較容易實現;函數
在C裏面也能夠實現, 有個strcat函數, 這裏還有一種方法是自定義鏈接字符串的函數spa
例一指針
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define LINE 4096 void rtrim(char *str){ char *s; s = str + strlen(str); while (--s >= str) { if (!isspace(*s)) break; *s = 0; } } int main(int argc, char * argv[]){ void rtrim(char *str); FILE *in; char *T1 = (char*)malloc(sizeof(char) *LINE); int i, a, seq_len, seq_number = 0; char fa[10240] = ""; for (i = 1; i < argc; i++){ printf("%s\n",argv[i]); rtrim(argv[i]); in = fopen(argv[i], "r"); if ( in == NULL){ printf ("Can't open %s for reading.\n", argv[i]); return 1; } while(!feof(in)){ fgets(T1, LINE, in); if (T1[0] != '>' ){ rtrim(T1); strcat(fa, T1); } else { seq_number ++; if(seq_number > 1){ printf("%s\n",fa); } printf ("%s",T1); memset(fa,0, 10240); } } printf("%s\n",fa); fclose(in); free(T1); } return 0; }
例二orm
#include <stdio.h> #include <stdlib.h> #include <string.h> #include <ctype.h> #define LINE 4096 void rtrim(char *str){ char *s; s = str + strlen(str); while (--s >= str) { if (!isspace(*s)) break; *s = 0; } } void concatStr(char *a, char *b){ while(*a++); a--; while((*a++ = *b++)!='\0'); } int main(int argc, char * argv[]){ void rtrim(char *str); void concatStr(char *a, char*b); FILE *in; char *T1 = (char*)malloc(sizeof(char) *LINE); int i, a, seq_len, seq_number = 0; char *pfa = (char*)malloc(sizeof(char) * 10240); for (i = 1; i < argc; i++){ printf("%s\n",argv[i]); rtrim(argv[i]); in = fopen(argv[i], "r"); if ( in == NULL){ printf ("Can't open %s for reading.\n", argv[i]); return 1; } while(!feof(in)){ fgets(T1, LINE, in); if (T1[0] != '>' ){ rtrim(T1); concatStr(pfa, T1); } else { seq_number ++; if(seq_number > 1){ printf("%s\n",pfa); } printf ("%s",T1); memset(pfa,0, 1024); } } printf("%s\n",pfa); fclose(in); free(T1); free(pfa); } return 0; }
兩個共同點是 都要用memset對指針清零, 不一樣點是第一個不用free(), 第二個要用free()釋放內存內存