需要帮助读取文件中的信息

发布于 2024-11-13 10:07:38 字数 2039 浏览 5 评论 0原文

所以我在链表中​​有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;
}

So I have chains of DNA letters (A,G,T,C) in linked list, and am supposed to read in from a file that looks like this:

I[tab]  ATT\n
I[tab]  ATC\n (etc)
L   CTA
L   CTG
V   GTA
V   GTG
F   TTT
F   TTC
..

where the single letters is what you get from the 3 a,t,g,c combination. I figured out how to start where I need to start (at the AGT), but can't formulate how to read the string and compare with the file to see what matches. This is what I have so far:

#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;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

无法回应 2024-11-20 10:07:38

函数 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 行代码,但没有奇怪的失败

The function fscanf() tends to create problems reading individual fields correctly since it has a sometimes strange idea of what a "%s" is.

Especially since this is a line-oriented file, use fgets() and then parse the string with sscanf, or even just looking through the characters one-at-a-time. Your code doesn't do that, so the simplified version is:

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)

    ...
}

This replaces 12 lines of your code, but without strange failures

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文