String with memory junk

Asked

Viewed 1,328 times

1

I’m having some problems working with files and functions, the code I’m making should print a string in the file, but this string is littered, and does not print out what should be used despite being used normally.

http://pastebin.com/JtGTDSeL

#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
#include <string.h>
#define MAIOR_ID 0



int top_IDS(int ID_DNA, char *linhas[])
{
    FILE *arquivo;
    arquivo=fopen("MEL_PIOR.txt","a");
    fflush(stdin);
    fputs(linhas,arquivo);
    fprintf(arquivo,"\t%i\n",ID_DNA);

    fclose(arquivo);

    return 0;
}


int calcular_peso(char lin_1[],int *qtd_g, int *qtd_c)
{
    int i=0;
    int soma_pesos=0,I_D=0;
    int qt,qt2;
    gets(lin_1);
    for(i=0; i<10; i++)
    {   if(lin_1[i]=='A' || lin_1[i]=='T')
            soma_pesos+=3;
        else
            soma_pesos+=7;
    }
    qt = *qtd_g;
    qt2 = *qtd_c;
    I_D=(soma_pesos+qt+qt2);
    printf("\tI_D: %i\n",I_D);
    if(I_D<50)
        printf("\tTem propencao a doencas cardiacas\n");
    else if(I_D>50)
        printf("\tTem propencao a doencas respiratorias\n");
    else
        printf("\tNada se pode afirmar sobre suas propencao as doencas \n");
    top_IDS(I_D,&lin_1);


    return 0;
}

int habilidades(int soma_guanina,int soma_adenina)
{
    if(soma_adenina>10)
        printf("\tTem propensäo a atividades esportivas\n");
    else if(soma_guanina>10)
        printf("\tTem propensäo a atividades artisticas\n");
    else
        printf("\tNada se pode afirmar sobre suas habilidades\n");
    return 0;
}

int cria_complementar(char ler_linha[],char complementar[])
{
    int contador=0;
    int qtd_A=0,qtd_T=0,qtd_C=0,qtd_G=0;
    int soma_AT=0,soma_CG=0;
    for(contador=0; contador<10; contador++)
    {
        if(ler_linha[contador]=='A')
        {
            complementar[contador]='T';
            qtd_A++;
        }
        else if(ler_linha[contador]=='T')
        {
            complementar[contador]='A';
            qtd_T++;
        }

        else if(ler_linha[contador]=='C')
        {
            complementar[contador]='G';
            qtd_C++;
        }

        else
        {
            complementar[contador]='C';
            qtd_G++;
        }

    }
    printf("\t");
    soma_AT=(qtd_A+qtd_T);
    soma_CG=(qtd_C+qtd_G);
    for(contador=0; contador<10; contador++)
    {
        printf("%c",complementar[contador]);
    }
    printf("\n\tA:%i T:%i C:%i G:%i",soma_AT,soma_AT,soma_CG,soma_CG);
    printf("\n\tA:%i%% T:%i%% C:%i%% G:%i%%\n\n",(soma_AT*100)/20,(soma_AT*100)/20,(soma_CG*100)/20,(soma_CG*100)/20);
    habilidades(soma_CG,soma_AT);
    calcular_peso(ler_linha,&qtd_G,&qtd_C);
    return 0;
}

int main()
{
    char bases[11],base_2[11];
    FILE *arquivo;
    arquivo = fopen("DNAS.txt","r");

    if(arquivo==NULL)
    {
        printf("O arquivo nao pode ser lido\n");
        system("pause");
        return 0;
    }


    while(fgets(bases,11,arquivo)!=NULL)
    {
        printf("\n");
        fgetc(arquivo);
        printf("\t%s\n",bases);
        cria_complementar(bases,base_2);
        system("pause");
        system("cls");
    }

    fclose(arquivo);


    return 0;
}

Reading the file is in the following format:

CGATGCATGC

Multiple lines using only ATCG and the print in the file is the same line followed by a number.

  • Can you be more specific in the problem you’re having? It’s tricky to help without knowing exactly what the problem is. It’s hard to find time to stay thrashing a code you’ve never seen to find a possible error somewhere. http://answall.com/help/mcve

1 answer

2


First thing I noticed

int top_IDS(int ID_DNA, char *linhas[])
{
    FILE *arquivo;
    arquivo=fopen("MEL_PIOR.txt","a");
    fflush(stdin);
    fputs(linhas,arquivo);
    ...

0) Do not check whether the fopen() worked properly
0) the fflush(stdin); does not seem to have connection with the rest of the code of this function
1) fputs() accepts a string and a FILE*, but linhas is not a string

Without reading the rest of the code I don’t know how to solve.
Turn on as much of warnings that your compiler allows. In principle he should warn that the call to fputs() is incorrect.


Or flames fputs() with each of the elements of linhas

for (k = 0; k < nlinhas; k++) fputs(linhas[k], arquivo);

Or re-defines the function to accept a string instead of an array of strings

int top_IDS(int ID_DNA, char *linhas)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.