Thread: carriage return when tokenizing

  1. #1
    Registered User
    Join Date
    Oct 2001
    Posts
    25

    carriage return when tokenizing

    Hi,

    I am using fgets() to read in a series of lines from a text file, then splitting this into tokens using strtok(). These tokens are then compared to an array of other tokens, and printed if the tokens match.
    However, because of the carriage return at the end of a line, whenever the last token in the line is printed, a carriage return is also printed. Is there anyway that I can print the token but not the carriage return?

    Thanks

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    > Is there anyway that I can print the token but not the carriage return?
    Have you tried making \n one of the chars which strtok uses to separate tokens
    Code:
    strtok( buff, " \n" );
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Unregistered
    Guest
    If you could please show us some code it would greatly be appreciated.

  4. #4
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    Am using....

    while (fgets(line, sizeof(line), openedInFile) != NULL) {
    splitTokens(line);
    }

    to open file and send each individual line to splitTokens, which has (char string[]) as it parameters. I am then using...

    x = strtok(string, " ");

    to tokenize it, looping through the string using...

    x = strtok(NULL, " ");

    there is an "if" statement in there comparing x to an array of tokens, printing x if it matches one of the elements. This works fine, except when printing the last token of a line, as the carriage return also prints, so the output is difficult to read.


    On a separate note, when the line bgins with a tab, the whole thing goes wrong. Any help would be appreciated!!!

    Thanks,

    Bill

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    Code:
    
    #include <stdio.h>
    #include <string.h>
    
    int main ( ) {
        char buff[] = "\t\thello world\n";
        char toks[] = "\t \n"; // tabs, spaces and newlines
        char *p;
        for ( p = strtok(buff,toks) ; p != NULL ; p = strtok(NULL,toks) ) {
            printf( "'%s'\n", p );
        }
        return 0;
    }
    

  6. #6
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    thanks mate, i think i understand the strtok a lot more now. I'll give your suggestion a go but i think it will work. can't understand why your fontsize is so big though!!!!

    Bill

    PS. Can I ask how old you are and how long it took you to become such an expert?

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    25
    if I have char delim[] = "\t " then the prog. works fine, but when I add \n to it, (ie: char delim[] = "\t in") it compiles but I get the message that the programme has performed an illegal operation at run-time and then the whole thing shuts down. Any ideas?

    Thanks, Bill

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. New string functions
    By Elysia in forum C Programming
    Replies: 11
    Last Post: 03-28-2009, 05:03 AM
  2. Code review
    By Elysia in forum C++ Programming
    Replies: 71
    Last Post: 05-13-2008, 09:42 PM
  3. DirectInput help
    By Muphin in forum Game Programming
    Replies: 2
    Last Post: 09-10-2005, 11:52 AM
  4. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  5. opengl code not working
    By Unregistered in forum Windows Programming
    Replies: 4
    Last Post: 02-14-2002, 10:01 PM