Read a specific sentence from a file and stop at it

0

I want to store a part of a file in a string : 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

From this 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 this part, or a way to do it if possible, please.

#include <stdio.h>

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

}

in the .txt file 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 get the names and save them in a string and the number in vectors or matrices, my goal is to present rankings and total scores, so I need to get the values correctly for do that. Someone please when I start doing the source code put the libraries and if it is int main () , or another please thank.

    
asked by anonymous 20.08.2016 / 03:36

1 answer

0

There is a way to do this by using fscanf in conjunction with 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 to buffer (be aware that this code is susceptible to overflow). When this fscanf reads a word within file , it will store in buffer from the point saved by ptr and returns 1. If it can read the word, ptr is updated to after% word and a space is added, thus allowing you to store the next word. After all words are read, it returns 0 and the execution point exits while . Finally, as the last iteration adds an extra space at the end of the sentence, the last space is removed.

    
20.08.2016 / 17:56