简体   繁体   中英

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 to allocate buffer");
        exit(1);
    }

    printf("Type something: ");
    characters = getline(&buffer,&bufsize,stdin);
    printf("%zu characters were 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 is

Type something: rosie is; really good
22 characters were read.
You typed: rosie is; really good
token : rosie is 
outside count ------------------------1
insdie count 1
AWAVA��AUATL�% ------------------- 
insdie count 2
is------------------- 
outside count ------------------------2

A number of basic fixes are needed or have been applied in the code below:

  • Let getline() do the memory allocation.
  • Check that getline() read a line.
  • Free the memory allocated.
  • Set token2 with inner strtok_r() call.
  • Make variable names a bit more systematic.
  • Make characters into ssize_t to match the return from getline() .
  • Print characters using %zd . This is marginally controversial; it prints the signed variant of size_t with the z -qualified signed decimal format string. It makes sense (to me at least and my compiler — GCC 6.2.0 on macOS Sierra), but I'm not sure where to find the confirmation that it is officially sanctioned by (the POSIX) standards.
  • Renaming variables for consistency.
  • Other minor cosmetic fixups (the print formatting could still be improved — quite a 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 were 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; really good
22 characters were read.
You typed: rosie is; really good
token: rosie is 
outside count ------------------------1
inside count 1
rosie------------------- 
inside count 2
is------------------- 
outside count ------------------------2
inside count 3
really------------------- 
inside count 4
good
------------------- 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM