Solved

strtok()

Posted on 2007-04-02
10
602 Views
Last Modified: 2008-02-01
here is a program  to make tokens whenever there is a  comma , fullstop space . i want to include tab also in the list . That is , when there is a tab also ,it should make token . How can i make it  ?

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

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
0
Comment
Question by:ratheesh_k
  • 6
  • 4
10 Comments
 
LVL 53

Expert Comment

by:Infinity08
ID: 18841411
>>   pch = strtok (str," ,.-");

Add \t to the list :

  pch = strtok (str," ,.-\t");
0
 

Author Comment

by:ratheesh_k
ID: 18841423
Actullay i am  copying some data from Excel sheet and pasting it in notepad . I am reading the file line by line and feed to the program . But there is a problem . I tried with \t also . ..
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18841433
What's the problem ? Can you describe it as accurate as possible - with example data, and code, and error messages ?
0
Has Powershell sent you back into the Stone Age?

If managing Active Directory using Windows Powershell® is making you feel like you stepped back in time, you are not alone.  For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why.

 

Author Comment

by:ratheesh_k
ID: 18956479
i copy pasted from excel sheet to notepad ,and

12 34 56  78
66 23 44 53

i use strtok to read the file and make it tokens line by line . But when i typed all these in notepad ,there is no prob . But when i copy paste ,its behaving deiffernet ,,,,
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18956619
In your example, I see at least once that there are two spaces next to each other. You need to take that possibility in account.

ie. if your token is of size 0, then get the next ... or something similar.

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

int main ()
{
  char str[] ="- This, a sample string.";
  char * pch;
  printf ("Splitting string \"%s\" into tokens:\n",str);
  pch = strtok (str," ,.-");
  while (pch != NULL)
  {
    if (strlen(pch)) printf ("%s\n",pch);
    pch = strtok (NULL, " ,.-");
  }
  return 0;
}
0
 

Author Comment

by:ratheesh_k
ID: 18956700
i m using the same above code .
0
 
LVL 53

Accepted Solution

by:
Infinity08 earned 50 total points
ID: 18956711
No, I made a small modification to your code - namely on this line :

    if (strlen(pch)) printf ("%s\n",pch);
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18956713
If that doesn't fix your problem, can you tell me what difference you see between the text file you created manually and the text file generated from Excel ?
0
 

Author Comment

by:ratheesh_k
ID: 18956727
that maynot be the problem ,when i type a txt file ,i an keep enough space bet two numbers . That is not a problem . i hexdumped the two files .

In one ,trhere is a (ascii of \t ) . In other one there is not . So i included \t in strtok param . But still it is hing some problem .
0
 
LVL 53

Expert Comment

by:Infinity08
ID: 18956732
>> But still it is hing some problem .

Which problem ? Can you describe it ? Can you also show your exact code as well as example files ?
0

Featured Post

ScreenConnect 6.0 Free Trial

Explore all the enhancements in one game-changing release, ScreenConnect 6.0, based on partner feedback. New features include a redesigned UI, app configurations and chat acknowledgement to improve customer engagement!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

This tutorial is posted by Aaron Wojnowski, administrator at SDKExpert.net.  To view more iPhone tutorials, visit www.sdkexpert.net. This is a very simple tutorial on finding the user's current location easily. In this tutorial, you will learn ho…
Examines three attack vectors, specifically, the different types of malware used in malicious attacks, web application attacks, and finally, network based attacks.  Concludes by examining the means of securing and protecting critical systems and inf…
The goal of this video is to provide viewers with basic examples to understand opening and writing to files in the C programming language.
The goal of this video is to provide viewers with basic examples to understand and use conditional statements in the C programming language.

821 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question