Search code examples
cstringprintingtoken

How to print part of a tokenized string from a file in C


I am having some trouble with a tokenized string that is a line from a file. I want to print the line from where the token is found, but I cannot seem to find a way around it. Please, ignore the output to the file part as well as the author, class and method if statements as I have them sorted out.

For example, I want it to print from this line: @return the matric only this part: the matric

Code:

#include <stdio.h>
#include <string.h>

int main (int argc, char **argv)
{
    char line [1000];
    char *delimeters = ".,; \t\n";
    int total_lines = 0;
    int total_comments = 0;
    int nonblank_lines = 0;

    FILE *input = fopen (argv[2], "r");
    FILE *output = fopen (argv[4],"w");
    
    while(fgets(line,1000,input) != NULL)
    {
        char *word = strtok(line, delimeters);
        total_lines++;
        
        if(word != NULL)
        {
            nonblank_lines++;
        }
        
        if(word != NULL && strcmp(word,"/**") == 0)
        {
            total_comments++;
        }
        
        while(word != NULL)
        {
            if(word != NULL && strcmp(word,"@author") == 0)
            {
                char *author_name = strtok(NULL, delimeters);
                char *author_surname = strtok(NULL, delimeters);
                printf ("Author: %s %s\n", author_name, author_surname);
            }
        
            if(word != NULL && strcmp(word,"public") == 0)
            {
                char *jmp = strtok(NULL, delimeters);
                    
                if(jmp != NULL && strcmp(jmp,"class") == 0)
                {
                    char *class_name = strtok(NULL, delimeters);
                    printf ("Class %s\n", class_name);
                }else{
                    char *method_name = strtok(NULL, delimeters);
                    printf ("Method %s\n", method_name);
                }
            }
            
            if(word != NULL && strcmp(word,"@return") == 0)
            {
                printf("Enters IF 4\n");
                
                char *return_value = strtok(NULL, delimeters);
                
                printf ("Returns: %s\n", return_value;
            }
        
            /*if(word != NULL && strcmp(word,"@param") == 0)
            {   
                printf("Enters IF 5\n");
                char *parameters = strtok(NULL, delimeters);
                printf("Parameter: %s\n", parameters);
                
                //int param_found
                
            }*/
            word = strtok(NULL, delimeters);
        }
    }
    
    printf ("The total number of lines is %d\n", total_lines);
    printf ("The total number of  non-blank lines is %d\n", nonblank_lines);
    printf ("The total number of comments is %d\n", total_comments);
    
    fclose(input);
    fclose(output);
    return 0;
}

Solution

  • So following my comment, you'd want something like this block for each if statement:

            while(fgets(line, 1000, input) != NULL)
            {
                char *first_word_in_line = strtok(line, delimeters);
    
                if(strcmp(first_word_in_line, "@return") == 0)
                {
                    char *word = strtok(NULL, delimeters);
                    printf ("Returns: ");
                    while(word != NULL)
                    {
                        printf ("%s ", word);
                        word = strtok(NULL, delimeters);
                    }
                    printf("\n");
                }
            }
    

    Note that I added another variable first_word_in_line on top word - it's not a must but it makes it less confusing when you code and it shows that the first word in the line has a different meaning because that's just the title.

    Also, you should read about strcmp vs. strncmp. Usually it's a good practise to use strncmp.