c - Wireless tools: Converting network essid to char -
i need correcting code because prints out weird stuff.
i used wireless tools , iwlib.h scan wireless networks , essids. when use:
printf("network name %s:", result->b.essid);
then works charm , prints name out me. however, want convert char can later send on buffer through network.
(unless can send results , "name extraction" can happen on other side? or not possible?)
see sample code below (not full code) of how attempt random characters result.
wireless_scan_head head; wireless_scan *result; iwrange range; while(result != null) { char *network; network = result->b.essid; int k; int size = strnlen(result->b.essid); printf("\n network essid:"); for(k=0; k<=size; k++) { printf("%c", network[k]); k++; } result = result->next; }
thanks help!
just formalize things...
you're incrementing k inside loop in declaration. outputting every other character , reading past bounds of network[] too. (you said tired... that'll it!)
another thing, you're using strnlen function, without specifying max length. strongly suggest compiling -wall -wextra , possibly -wunused show problems. nit picky , use -werror too, fix warnings , errors shown, you'll write better code , build better habits.
another thing, if can use strlen (or strnlen) function determine length, loop redundant. simple: (as pointed out)
printf("network essid: %s\n", result->b.essid);
would suffice.
essid defined in struct either 'char essid[xx];', or (better) 'char* essid;', implies it's already char string. (a 'char string' in c not stored same way 'string string' in c++)
{grin} if really want write that:
while (result != null) { char *network = result->b.essid; /* string length unsigned */ size_t size = strlen(network); /* need unsigned index compare */ unsigned int k; /* initial part of output */ printf("network essid: "); (k = 0; k <= size; ++k) { /* printf overkill single char */ putchar(network[k])); } /* terminating cr/lf/crlf, printf overkill again */ puts(null); result = result->next; }
and short version:
while (result != null) { printf("network essid: %s\n", result->b.essid); result = result->next; }
best wishes on code!
Comments
Post a Comment