Here I want to create dynamic memory. here i dnt know the size of the output and i want to print the last final result after the while loop.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main() {
char *sdpCommand = "sdptool browse 04:18:0F:B1:48:B5";
FILE *fp;
fp = popen(sdpCommand, "r");
char *results = 0;
if (fp == NULL) {
printf("Failed to run command\n");
return;
}
char* buffer = malloc(514);
while (fgets(buffer, strlen(buffer) - 1, fp) != NULL) {
realloc(results, strlen(results) + strlen(buffer));
memcpy(results + strlen(results), buffer, strlen(buffer));
}
printf("Output ::: %s", results);
pclose(fp);
sleep(1);
}
source
share