Thread: Removing trailing character with strtok

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    178

    Removing trailing character with strtok

    Hello everyone,
    I am having a problem with the output of a file. My function does everything it needs to do with the exception of one line. I have a .dat file with the following:
    Code:
    joe     clint james brint howard
    jimmy
    alexander
            *
    me
    When I run the function my output is as such:
    Code:
    file opened
    word: joe
    digit: 3
    word: clint
    digit: 5
    word: james
    digit: 5
    word: brint
    digit: 5
    word: howard
    
    digit: 7    <---------------------------------Here is the problem
    word: jimmy
    digit: 5
    word: alexander
    digit: 9
    ERROR non alphanumeric digit on line 4!
    File closed
    "Howard" is only six digits in length. Also it seems to skips a line in the output which could be the extra character but I am not sure. What I find very interesting is the other names, (jimmy & alexander) on separate lines do not have that problem.
    Here is my function:
    Code:
    #include "main.h"
    #include "fileCheck.h"
    
    int fileCheck(FILE *fp)
    {
    
        int line_count = 0;
        int ret_val = 0;
        int digit_count = 0;
        char file[BUFF];
        regex_t regex;
        char *pch;
    
        if (regcomp(&regex, to_find, REG_EXTENDED | REG_NEWLINE) != 0)
        {
            fprintf(stderr, "Failed to compile regex '%s'\n", to_find);
            return EXIT_FAILURE;
        }
    
    
        while (fgets(file, BUFF, fp) != NULL)
        {
    
            ++line_count;
    
            if ((ret_val = regexec(&regex, file, 0, NULL, 0)) == 0)
            {
                printf("ERROR non alphanumeric digit on line %d!\n", line_count);
                return EXIT_FAILURE;
            }
    
            pch = strtok(file, " \n\r\t");
            while (pch != NULL)
            {
                digit_count = strlen(pch);
                printf("word: %s\n", pch);
                printf("digit: %d\n", digit_count);
                pch = strtok(NULL, " ");
            }
    
        }
    
    }
    I for the life of me cannot figure it out. Any ideas on how I can correct this? I am aware of sscanf to parse the line but I do not know the format of the file. All I know is the file will be alphanumeric and if there is a non-alphanumeric, I must print out an error with the line it occurred on such as the above output.
    Thanks for your help.
    Last edited by csharp100; 09-18-2013 at 07:03 PM. Reason: better explanation

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    You just need to use " \n\r\t" in your second strtok too.
    And your error message should probably say "alphanumeric character" not "alphanumeric digit".
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  3. #3
    Registered User
    Join Date
    Jul 2010
    Posts
    178
    Quote Originally Posted by oogabooga View Post
    You just need to use " \n\r\t" in your second strtok too.
    And your error message should probably say "alphanumeric character" not "alphanumeric digit".
    I just saw that in my program and came back here to correct it. Kudos and thanks for the reply.

  4. #4
    Stoned Witch Barney McGrew's Avatar
    Join Date
    Oct 2012
    Location
    astaylea
    Posts
    420
    If you just want to read through a stream, word by word, then what you have is overkill. You can just use fscanf with the 's' format specifier and it will read a word into your buffer, skipping all whitespace. Here's an example:

    Code:
    #include <stdio.h>
    
    int main(void)
    {
        char buf[1024];
    
        while (scanf("%1023s", buf) == 1)
            puts(buf);
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. REmoving a character from a character array
    By Bladactania in forum C Programming
    Replies: 3
    Last Post: 02-11-2009, 02:59 PM
  2. Removing leading and trailing whitespace
    By JimpsEd in forum C Programming
    Replies: 2
    Last Post: 05-14-2006, 03:55 PM
  3. Removing Leading & Trailing Whitespace
    By EvilGuru in forum C Programming
    Replies: 11
    Last Post: 12-01-2005, 02:59 PM
  4. Using fgets, removing last character
    By Stack Overflow in forum C Programming
    Replies: 31
    Last Post: 07-07-2004, 05:38 PM
  5. k&r ex1-18 removing trailing spaces.
    By xion in forum C Programming
    Replies: 1
    Last Post: 07-14-2003, 02:20 PM