How to put score in a sql Row

Asked

Viewed 114 times

1

Well I am looking for a way to put score on a score of Rows that is made from my mysql.

If the returned value is above 1000 it insert scores, for example:

  • 1000 = 1,000
  • 1000000 = 1,000,000

The code is as follows:

void sql_rows(MYSQL_ROW sql) {
   printf("a quantidade de rows é '%s'\n", sql->row[0]);
}

I always get the most scoreless values it is possible to add a score in the returned value of Rows ?

  • You need to give more context to what you’re doing, provide more code.

  • plus the rest of the code is useless, because it has nothing that can help with that, and this MYSQL_ROW is from the mysql API. the very point is that the Rows returns as a string, if it were integer it would be easier.

1 answer

0

You can create a function that writes the number to pieces, and insert the dots between them:

void
escrever_numero(unsigned int n, FILE * stream) {
    char buf[10];
    char * ptr = buf;
    size_t nchar, i, j;

    // escreve número no buffer
    nchar = sprintf(buf, "%d", n);
    // Quantos dígitos preciso escrever para a primeira classe?
    i = nchar % 3; if (!i) i = 3;
    // escrever a primeira classe
    fprintf(stream, "%.*s", i, ptr);
    // avançar
    ptr += i;
    // enquanto ainda houver classes para escrever,
    // escreva um ponto e depois a classe
    while (*ptr) {
        fputc('.', stream);
        fprintf(stream, "%.3s", ptr);
        // não esqueça de avançar o ponteiro
        ptr += 3;
    }
}

Then just put it in the place of printf() call to this function:

void sql_rows(MYSQL_ROW sql) {
    puts("a quantidade de rows é ");
    escrever_numero(sql->row[0], stdout);
    puts("\n");
}

Note that the function I wrote only works with non-negative integers, but this is for the specific case of line counting (or anything else). If you need a function that handles negative numbers, just treat the possibility of a minus sign before the first class.

Finally, it is also possible to replace this parameter FILE * by a char * and exchange calls to fprintf() for sprintf(), if you want, but be careful to size up your arrays correctly and also finish the string with a \0, that does not need to do with the FILE *.

Browser other questions tagged

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