c - Nested strtok_r : command line argument parsing -
code here: http://ideone.com/aznxfm
#include <stdio.h> #include <stdlib.h> #include<string.h> int main() { char *buffer; size_t bufsize = 32; size_t characters; buffer = (char *)malloc(bufsize * sizeof(char)); if( buffer == null) { perror("unable allocate buffer"); exit(1); } printf("type something: "); characters = getline(&buffer,&bufsize,stdin); printf("%zu characters read.\n",characters); printf("you typed: %s",buffer); char *end_str,*token2; char *token = strtok_r(buffer,";",&end_str); printf("token : %s \n", token); int count =0,wordcnt=0; while(token !=null) { char *end_token; count++; printf("outside count ------------------------%d\n", count); strtok_r(token," ",&end_token); while(token2!=null) { wordcnt++; printf("insdie count %d\n",wordcnt); printf("%s------------------- \n", token2); token2 = strtok_r(null," ",&end_token); } token = strtok_r(null, ";",&end_str); } return(0); }
output
type something: rosie is; 22 characters read. typed: rosie is; token : rosie outside count ------------------------1 insdie count 1 awava��auatl�% ------------------- insdie count 2 is------------------- outside count ------------------------2
a number of basic fixes needed or have been applied in code below:
- let
getline()
memory allocation. - check
getline()
read line. - free memory allocated.
- set
token2
innerstrtok_r()
call. - make variable names bit more systematic.
- make
characters
ssize_t
match returngetline()
. - print
characters
using%zd
. marginally controversial; prints signed variant ofsize_t
z
-qualified signed decimal format string. makes sense (to me @ least , compiler — gcc 6.2.0 on macos sierra), i'm not sure find confirmation officially sanctioned (the posix) standards. - renaming variables consistency.
- other minor cosmetic fixups (the print formatting still improved — quite lot).
leading to:
#include <stdio.h> #include <stdlib.h> #include <string.h> int main(void) { char *buffer = 0; size_t bufsize = 0; ssize_t characters; printf("type something: "); characters = getline(&buffer, &bufsize, stdin); printf("%zd characters read.\n", characters); if (characters > 0) { printf("you typed: %s", buffer); char *end_str1; char *token1 = strtok_r(buffer, ";", &end_str1); printf("token: %s \n", token1); int count = 0, wordcnt = 0; while (token1 != null) { char *end_str2; count++; printf("outside count ------------------------%d\n", count); char *token2 = strtok_r(token1, " ", &end_str2); while (token2 != null) { wordcnt++; printf("inside count %d\n", wordcnt); printf("%s------------------- \n", token2); token2 = strtok_r(null, " ", &end_str2); } token1 = strtok_r(null, ";", &end_str1); } } free(buffer); return(0); }
example run:
type something: rosie is; 22 characters read. typed: rosie is; token: rosie outside count ------------------------1 inside count 1 rosie------------------- inside count 2 is------------------- outside count ------------------------2 inside count 3 really------------------- inside count 4 -------------------
Comments
Post a Comment