here i want to run this program and take one character pointer like char *finalstring.
but how can i allocate memory to this finalstring.
i cant declare like 512 bytes and 1024 bytes etc. because here output not fix its depend on Bluetooth address.
So any one can tell me i want to store these all output data which i prints here in one character pointer. i want to concat every while loop execution and want to declare dynamic memory.
code :
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
void main() {
char *sdpCommand = "sdptool browse 04:18:0F:B1:48:B5";
FILE *fp;
char* finalstring = malloc(512);
char result[512];
fp = popen(sdpCommand, "r");
if (fp == NULL) {
printf("Failed to run command\n");
return;
}
if (finalstring) {
*finalstring = 0;
/* Read the output a line at a time - output it. */
while (fgets(result, sizeof(result) - 1, fp) != NULL) {
char* newfinalstring = realloc(finalstring, strlen(result) + 1);
if (newfinalstring) {
finalstring = newfinalstring;
strcat(finalstring, result);
}
printf("Output ::: %s", result);
}
free(finalstring);
}
printf("\nfinal Output ::: %s", finalstring);
free(finalstring);
/* close */
pclose(fp);
sleep(1);
}
output :
Output ::: Browsing 04:18:0F:B1:48:B5 ...
Output ::: Service RecHandle: 0x10000
*** glibc detected *** ./sdptool: realloc(): invalid next size: 0x00011008 ***
======= Backtrace: =========
/lib/libc.so.6[0x400a2f3c]
/lib/libc.so.6[0x400a72ac]
/lib/libc.so.6(realloc+0x108)[0x400a84ec]
./sdptool[0x86d4]
/lib/libc.so.6(__libc_start_main+0x120)[0x4004efd4]
======= Memory map: ========
00008000-00009000 r-xp 00000000 1f:04 16647 /opt/WL1271_demo_01/bluetooth_scripts/sdptool
00010000-00011000 rwxp 00000000 1f:04 16647 /opt/WL1271_demo_01/bluetooth_scripts/sdptool
00011000-00032000 rwxp 00000000 00:00 0 [heap]
40000000-4001d000 r-xp 00000000 1f:04 747 /lib/ld-2.8.so
4001d000-40021000 rwxp 00000000 00:00 0
40024000-40025000 r-xp 0001c000 1f:04 747 /lib/ld-2.8.so
40025000-40026000 rwxp 0001d000 1f:04 747 /lib/ld-2.8.so
40026000-40032000 r-xp 00000000 1f:04 762 /lib/libgcc_s.so.1
40032000-40039000 ---p 0000c000 1f:04 762 /lib/libgcc_s.so.1
40039000-4003a000 rwxp 0000b000 1f:04 762 /lib/libgcc_s.so.1
4003a000-40157000 r-xp 00000000 1f:04 753 /lib/libc-2.8.so
40157000-4015e000 ---p 0011d000 1f:04 753 /lib/libc-2.8.so
4015e000-40160000 r-xp 0011c000 1f:04 753 /lib/libc-2.8.so
40160000-40161000 rwxp 0011e000 1f:04 753 /lib/libc-2.8.so
40161000-40164000 rwxp 00000000 00:00 0
40200000-40221000 rwxp 00000000 00:00 0
40221000-40300000 ---p 00000000 00:00 0
bec5c000-bec71000 rw-p 00000000 00:00 0 [stack]
Aborted
You have to keep track of how long the string is currently, and how much is allocated. Then when adding more text, if the new length will be more than what’s allocated you allocate more.
Here’s some code you can use:
PS.
Don’t forget to clean up after you, by freeing both the buffer data and the buffer structure.
Edit
This code doesn’t contain any error checking or handling! Use of a debugger is highly recommended!