所以我在链表中有DNA字母链(A,G,T,C),我应该从一个看起来像这样的文件中读入:
I[tab] ATT\n
I[tab] ATC\n (etc)
L CTA
L CTG
V GTA
V GTG
F TTT
F TTC
..
其中单个字母是从3 a,t,g,c组合中得到的字母。我想出了如何开始我需要启动的地方(在AGT),但是无法制定如何读取字符串并与文件进行比较以查看匹配的内容。这就是我到目前为止所做的:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
typedef struct node{
char seq[300];
struct node* next;
} NODE;
int
main(int argc, char* argv[]){
int i, j=0;
FILE *fin, *fout, *fop;
char code1[300], code2[300], prot;
NODE *current, *first, *prev;
fin = fopen( argv[1], "r");
fout = fopen( argv[2], "w");
fop = fopen("codeoflife.txt", "r");
current = first = malloc (sizeof (NODE));
while( fscanf( fin, "%s", current -> seq) != EOF) {
for (i = 0; i < 300; i++){
if (current->seq[i] == 'a')
current->seq[i] = 'A';
else if (current->seq[i] == 't')
current->seq[i] = 'T';
else if(current->seq[i] == 'g')
current->seq[i] = 'G';
else if(current->seq[i] == 'c')
current->seq[i] = 'C';
}
if ( (current -> next = malloc ( sizeof(NODE) ) ) == NULL){
fprintf(fout, "Out of memory\nCan't add more DNA sequences\n");
return EXIT_FAILURE;
}
prev = current;
current = current -> next;
}
free(current)
prev->next = NULL;
current = first;
while(current->next != NULL){
for( i = 0; i < 300; i++){
if( current->seq[i] == 'A')
if( current->seq[i+1] == 'G')
if( current->seq[i+2] =='T'){
code1[j] = 'M';
while(fscanf(fop, "%c", &prot)) != EOF){
break;
}
if (i == 299)
strcpy ( current->seq, "None");
current = current->next;
}
return 0;
}
答案 0 :(得分:0)
函数fscanf()
往往会在正确读取单个字段时产生问题,因为它有时会对“%s”有什么奇怪的想法。
特别是因为这是一个面向行的文件,使用fgets()
然后用sscanf解析字符串,或者甚至只是一次查看字符。您的代码不会这样做,因此简化版本为:
while (fgets (current->seq, sizeof (current->seq), fin))
{
char *cp = strchr (current->seq, '\n'); // fgets usually adds \n
if (cp) // if \n present
*cp = '\000'; // remove \n
strupr (current->seq); // make all upper case (unifies mixed case input)
...
}
这将替换12行代码,但没有奇怪的失败