How to catch the line below (next line) in C?

Asked

Viewed 241 times

0

Good morning people, I have a file that contains several lines, among them 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 take the third line shown there, the second in which appears "REMARK". More specifically, the last numerical value of this line, the value "933.999".

I’m doing like 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 wanted to know if there is something to pick up the bottom line, something like "if line start == Helix, fgets+1", or whatever. I think you can do with fgets, but I do not know how. Someone can help me?

Any other suggestion is also welcome! =)

  • 1

    Apparently not, if I understand correctly. fgets() will read bytes, that’s all, you have to create the algorithm you want with the information read.

1 answer

2

If I understand correctly, you want to take the last value of the third line, well, to make the comparison of the initial text, you can use the memcmp, it makes a comparison of bytes similar to the strcmp, the difference is that you can specify the size of the bytes to be checked.

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.
}
  • 1

    The strcmp used to compare strings, already the strstr looks like looking for a value inside a string, as I understand it. Referential here Tutorial Point

  • and its return value is the position of the string at which the sequence of the user starts (if the sequence is found).

Browser other questions tagged

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