execute grep command in C language and return result

-2
void main()
{
    char comando[2000];
    char resultado[500]
    sprintf(comando, "grep '[0-9]{50}-' input.txt);
    resultado = system(comando);

    printf("%s\n",resultado);
}

I need only 1 match, and return the value in result, the above code does not compile, it's just an example of how you'd like it to be.

    
asked by anonymous 19.12.2017 / 15:04

1 answer

0
#include <stdio.h>
#include <stdlib.h>

FILE *popen(const char *command, const char *mode);
int pclose(FILE *stream);

int main(void)
{
FILE *cmd;
char result[1024];

cmd = popen("grep bar /usr/share/dict/words", "r");
if (cmd == NULL) {
perror("popen");
exit(EXIT_FAILURE);
}
while (fgets(result, sizeof(result), cmd)) {
printf("%s", result);
}
pclose(cmd);
return 0;
}
That was exactly what I was looking for. Thank you.

    
20.12.2017 / 09:32