How to get the line below (next row) in C?

0

Good morning people, I have a file that contains several lines, including the following:

REMARK Evaluations: CoarseFF Proper-dihedrals   Coarse Atomic Repulsion Coarse Compaktr Hbond_Strands   Hbond_Angle_Dist_var_power_Strands  Hbond_AHelixes  
Helix_packing_coarse    HB_COOPER_coarse    RR_contacts_coarse  Total Energy  
REMARK 914959: 119.706  63.2753 86.7273 26  237.053 28  2218.95 -48976  13.5677 933.999  

I need to get the third line shown there, the second where "REMARK" appears. More specifically, the last numerical value of this line, the value "933.999".

I'm doing this:

while(!feof(arquivo))
{
    fgets(linha_do_arquivo, 1000, arquivo);
    if( (strstr(linha_do_arquivo, "REMARK") == linha_do_arquivo) && (strstr(linha_do_arquivo, "Evaluations:") != linha_do_arquivo+7) )
    {
        sscanf(linha_do_arquivo, "%*s %*s %*s %*s %*s %*s %s", valor_numérico);
    }
}

However, I was wondering if there is anything to get the bottom line, something like "if line start == helix, fgets + 1", or whatever. I think I can do with fgets, but I do not know how. Can anyone help me?

Any other suggestions are also welcome! =)

    
asked by anonymous 04.12.2016 / 13:14

1 answer

2

If I understand correctly, you want to get the last value of the third line, well, to do the comparison of the initial text, you can use memcmp , it does a comparison of bytes similar to strcmp , the difference is which can specify the size of the bytes to check.

float valor;

if(!strcmp(linha_do_arquivo, "REMARK", 6)){ // verifica se tem a palava REMARK nas 6 primeiras posições do arquivo
    int x = strlen(linha_do_arquivo)-1;

    while(linha_do_arquivo[x] != ' '){ // posiciona x na posição do ultimo valor da linha
        x--;
    }

    sscanf(linha_do_arquivo+x, "%f", &valor); // passa o valor para a variável valor.
}
    
05.12.2016 / 02:54