0
I need something similar to the stringstream you have in the C++ connection for the C language, I need it a lot because I want to make querys in a database using C (C mysql Connector).
Since I had no idea what I had in the sstream library for C I thought of using strncat, but it didn’t come out as planned, note:
#include <string.h>
#include <stdio.h>
int main(){
    char username[25];
    char passwd[45];
    printf("Input your name ->");
    fgets(username, 25, stdin);
    printf("Input your password->");
    fgets(passwd, 45, stdin);
    char query[128]="select username, password from accounts where username='";
    char and[18]="' and password='";
    strncat(query, username, 25);
    strncat(query, and, 16);
    strncat(query, passwd, 45);
    strncat(query, "';\n", 4);
    printf("%s", query);
    return 0;
    //OBS: Código de exemplo
}
When I run the show:
gcc main.c
./a.out
Input your name ->linus
Input your password->123
select username, password from accounts where username='linus
' and password='123
';
Note that he skipped a line and gave some spaces and this in a query could generate error.
Wouldn’t it be better just in case, to check if the entrance was a whole line? If it is a truncated input (reached EOF, for example), I have the impression that the last character would not be
\n– Jefferson Quesado
@Jeffersonquesado Yes indeed would be playing it safe. I also agree that it is better. I will edit
– Isac
@Isac Exactly this information I needed. Thanks!
– user105951