简体   繁体   中英

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;
}

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 .

The answer I got is this:

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

You messed a bit with the parameters at the beginning.

./program.c input.txt output.txt

argv[0] will give you the name of the program (in this case program.c), argv[1] the first param you pass (input.txt) and argv[2] the second parameter (output.txt)

There was also a missing bracket and for a comment /* comment */ one star in c is enough.

That fixed it worked for me. But be aware if you cut the comment with strtok() the way you do it, there has to be a delimiter left and right to the start of your comment.

somecode; /* This should be recognized */
somecode;/* This not */
somecode; /*This neither */

Here ist the corrected 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[1], "r");
    FILE *output = fopen (argv[2],"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;
}

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