Hi, I am reading from a text file and stored it onto a string variable. Now I want to split the string and I tried looking it up on the web and found the strtok () function but that only works on a char type.
Here is my code snippet:

#include <iostream>
#include "Stack.h"
#include <string.h>
#include <fstream>
using namespace std;

int main()
{
 Stack list;
 ifstream ReadFrom;
 string line;

(getline(ReadFrom, line));
split = strtok(line.c_str(),",.( )") // this does not work.;

Can some one give me an idea on how to accomplish this. Note: variable line has to be string type unless there is another way to read whole line from a text file.

thanks.

Here is a good tutorial on using strtok(), it's written in C, but that's what you get for using c-strings teehee
http://cboard.cprogramming.com/cplusplus-programming/60924-cstring-algorithm-what-do-you-think.html

it offers this example:

/* strtok example */
#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;
}

Here is my first attempt at strtok()'ing:
http://cboard.cprogramming.com/cplusplus-programming/60924-cstring-algorithm-what-do-you-think.html

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.