0

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.

2
Contributors
1
Reply
2
Views
8 Years
Discussion Span
Last Post by Clinton Portis
0

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

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.