Read a specific phrase from a file and stop at it

Asked

Viewed 87 times

0

I want to store in a string a part of an archive: Bruno Mossa Rezende 5 2 7 6 22 22 0.13 8 19 141 0.17 260 2 320 5.42 43 22 6 0.90 0 0 0 5

of that file I want to store in a string "Bruno Mossa Rezende", I do not want the numbers at the moment, I want to know how to stop at this part, or a means to do this if possible, please.

#include <stdio.h>

int main()
{
    FILE *file;
    file = fopen("entrada.txt", "r");

}

in file . txt has it here:

15
Bruno Mossa Rezende         5   2   7   6   22  22  0.13    8   19  141 0.17    260 2   320 5.42    43  22  6   0.90    0   0   5
Douglas Souza               22  12  23  4   5   8   0.08    1   10  25  0.02    0   0   4   0.00    25  7   1   0.52    49  10  22
Eder Carbonera              30  6   19  15  44  19  0.31    9   16  75  0.19    0   0   3   0.00    14  9   1   0.28    5   3   2
Evandro M. Guerra           50  14  27  7   17  13  0.15    1   5   17  0.02    0   0   1   0.00    10  5   2   0.21    0   0   0
Isac Santos             31  8   8   7   20  14  0.15    1   14  44  0.02    0   0   3   0.00    3   2   1   0.06    0   0   0
Lucas Saatkamp              58  9   21  8   30  23  0.17    9   23  62  0.19    0   0   3   0.00    13  2   1   0.27    2   0   1
Luiz Felipe Marques Fonteles        27  7   17  4   14  8   0.08    3   7   41  0.06    0   0   3   0.00    19  9   2   0.40    40  8   33
Mauricio Borges Almeida Silva       80  20  54  3   28  21  0.06    11  18  68  0.23    0   0   4   0.00    28  20  4   0.57    93  22  83
Maurício Souza              46  5   22  28  41  26  0.57    5   11  109 0.10    0   0   3   0.00    13  5   2   0.27    2   1   3
Murilo Endres               14  4   22  2   7   6   0.04    4   7   16  0.08    0   0   4   0.00    12  9   0   0.25    30  0   28
Ricardo Lucarelli Santos De Souza   135 36  74  9   21  14  0.19    23  28  131 0.48    0   0   15  0.00    64  19  4   1.33    97  13  102
Sérgio Dutra Santos         0   0   1   0   0   0   0.00    0   0   0   0.00    0   2   26  0.00    52  23  4   1.08    55  19  46
Tiago Brendle               0   0   0   0   0   1   0.00    0   0   0   0.00    0   0   8   0.00    44  16  7   0.92    46  5   29
Wallace De Souza            160 40  90  23  32  32  0.48    8   25  91  0.17    1   0   8   0.02    43  24  8   0.90    0   1   0
William Arjona              1   0   0   0   5   8   0.00    1   2   54  0.02    92  0   118 1.92    21  10  2   0.44    0   0   1

I need to know how to take names and save them in a string and the numbers in vectors or matrices, my goal is to present rankings and total scores, and for that I need to get the values correctly to do this. Someone please when you start making the source code put in the libraries and if it is int main (), or other please thank you.

  • 2

    And what have you done? What is your specific question?

1 answer

0

There’s a way to do it using the fscanf together with the strlen. It would look something like this:

char buffer[/* coloque um tamanho razoável aqui*/];
char *ptr = buffer;
while (fscanf(file, "%s", ptr))
{
    ptr += strlen(ptr);
    *(ptr++) = ' ';
}
*(--ptr) = 0; // ptr agora aponta para o final da frase
              // buffer pode ser usado como uma string normal

How does this code work? Note that ptr always points inside the buffer (care that this code is susceptible to overflow). When this fscanf read a word inside the file, it will store in the buffer from the point saved by ptr and returns 1. If he can read the word, the ptr is updated to after the word and a space is added, thus allowing you to store the next word. After all words are read, it returns 0 and the run point leaves the while. Finally, as the last iteration adds an extra space at the end of the sentence, the last space is removed.

Browser other questions tagged

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