开发者

Problem when opening file AND possibly file i/o to string

I'm trying to write a program which will copy a file into a string (so far so good on that) which will contain DNA bases. The bases are then converted into proteins, first finding the first ATG sequence, then reading in sequences of 3 and convertind them, writing them to another file.

As of now, the program crashes before entering the first for loop.. And I don't know what's causing the problem.

int proteina(char DNA_origem[], char proteina_destino[]){
char aminocidosING [64][14]={"Isoleucine","Isoleucine","Isoleucine","Leucine","Leucine","Leucine","Leucine","Leucine","Leucine","Valine","Valine","Valine","Valine","Phenylalanine","Phenylalanine","Methionine","Cysteine","Cysteine","Alanine","Alanine","Alanine","Alanine","Glycine","Glycine","Glycine","Glycine","Proline","Proline","Proline","Proline","Threonine","Threonine","Threonine","Threonine","Serine","Serine","Serine","Serine","Serine","Serine","Tyrosine","Tyrosine","Tryptophan","Glutamine","Glutamine","Asparagine","Asparagine","Histidine","Histidine","Glutamic acid","Glutamic acid","Aspartic acid","Aspartic acid","Lysine","Lysine","Arginine","Arginine","Arginine","Arginine","Arginine","Arginine","Stop codons","Stop codons","Stop codons"};
char aminocidosPT [64][18]={"Isoleucina","Isoleucina","Isoleucina","Leucina","Leucina","Leucina","Leucina","Leucina","Leucina","Valina","Valina","Valina","Valina","Fenilalanina","Fenilalanina","Metionina","Cisteína","Cisteína","Alanina","Alanina","Alanina","Alanina","Glicina","Glicina","Glicina","Glicina","Prolina","Prolina","Prolina","Prolina","Treonina","Treonina","Treonina","Treonina","Serina","Serina","Serina","Serina","Serina","Serina","Tirosina","Tirosina","Triptofano","Glutamina*","Glutamina","Asparagina","Asparagina","Histidina","Histidina","Ácido glutâmico","Ácido glutâmico","Ácido aspártico","Ácido aspártico","Lisina","Lisina","Arginina","Argin开发者_开发技巧ina","Arginina","Arginina","Arginina","Arginina","Códons Stop","Códons Stop","Códons Stop"};
char codoes[64][3]={"ATT","ATC","ATA","CTT","CTC","CTA","CTG","TTA","TTG","GTT","GTC","GTA","GTG","TTT","TTC","ATG","TGT","TGC","GCT","GCC","GCA","GCG","GGT","GGC","GGA","GGG","CCT","CCC","CCA","CCG","ACT","ACC","ACA","ACG","TCT","TCC","TCA","TCG","AGT","AGC","TAT","TAC","TGG","CAA","CAG","AAT","AAC","CAT","CAC","GAA","GAG","GAT","GAC","AAA","AAG","CGT","CGC","CGA","CGG","AGA","AGG","TAA","TAG","TGA"};
char proteinas[64][1] = {"I","I","I","L","L","L","L","L","L","V","V","V","V","F","F","M","C","C","A","A","A","A","G","G","G","G","P","P","P","P","T","T","T","T","S","S","S","S","S","S","Y","Y","W","Q","Q","N","N","H","H","E","E","D","D","K","K","R","R","R","R","R","R",".",".","."};

/* a esta altura suponho que tenhas definido na main as strings dos aminoácidos*/


char **string1; 
FILE * ficheiro;
FILE * ficheiro_close;
int f_cmp; 
int k, i, start=0; /* variavel de comprimento */
char proteina_origem;

ficheiro = fopen(DNA_origem,"r"); /* DNA origem e a variavel onde ta guardada o nome do ficheiro do utilizador */
ficheiro_close = fopen(proteina_destino,"w+");

fscanf(ficheiro,"%c",string1); /* isto lê os conteudos da stream para a string, copiando pra lá o ficheiro. */

for(i=1;i<=f_cmp;i++) {

   if (strncmp(string1[i],codoes[15],3)==0) {
      fputs(proteinas[15],ficheiro_close);

      for(k=i+2;k<=f_cmp;k+3) {

         if ((strncmp(string1[k],codoes[k],3))==0) {
            fputs(proteinas[k],ficheiro_close);

            if (k==61&&k==62&&k==63) {
                return(0);
            }
          }
        }
      }
    }
}

Also, the compiler gives a warning if I don't use ** in char defining. Could you shed some light please? This should be just a simple project but I'm stuck in this last function..

Don't mind the var names and comments, it's in portuguese.

Thank you very much for your time!


If you are using pointers, then after declaration you have to initialize the pointer with suitable valid memory. After initialization, pointer is only pointing to an address, either to some other variable's address or the memory you create for this pointer (e.g. using malloc() or new operator). Better use a char array, if it does not harm your coding approach to accomplish you task at hand.

However if you declare string1 as

char** string1

use fscanf like this:

fscanf(ficheiro,"%c", *string1);


I believe one problem is with your fscanf call. You are attempting to scan a value and store it in the string1 variable, but there is no memory allocated for string1. As you have it now, string1 is just a pointer to a pointer to a char, so there is no place to store the actual string value. You could either allocate it on the stack, or allocate it on the heap. Try something like this:

char string1[80]; // Or use whatever size makes sense here

...

fscanf(ficheiro, "%c", string1);

Also, you might be doing this already, but you should fclose your FILEs when you're done with them.


This is wrong :

fscanf(ficheiro,"%c",string1);

I see string1 only declare but not initialized.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜