Link to home
Start Free TrialLog in
Avatar of meow00
meow00

asked on

C++ : Read a string, and break words by space ....


 Hi C++ Experts,

     I  want  to write a code, which read the first line of a file, and then determine how many columns are there in the first line (so will be in the file). The columns are separated by space. I wrote the following code, but it is kind of ugly and I don't like it ....... I am wondering if anyone could please suggest  some more elegant ways of doing this ? (Perhaps need to incorporate some script ?) I want all in one program, since I would need to declare something later in the code based on the number of columns in the file.

--------------------------------------------------------------------------
#include <fstream>
#include <iostream>
#include <string>

int main()
{
    ifstream input("Person.txt") ;
    ofstream output("temp_file.txt") ;
    string temp ;
    getline(input, temp) ;
    output << temp ;
    output.close() ;

    ifstream input1("temp_file.txt") ;
    cout << "the columns of the file are: " << endl ;
    while(input1){
     string temp1 ;
     input1 >> temp1 ;
     cout << temp1 << endl ;
    }
 
    system("rm temp_file.txt") ;

    return 0 ;
}
------------------------------
and the Person.txt looks like :

Name     Age     Height    Weight    Eye_colors
John     11      5'2"       100lbs    brown
Mary     15      6'         150lbs    green
Dave     12      5'5"       140lbs    blue
Avatar of jkr
jkr
Flag of Germany image

You could either use 'strtok()'

/* STRTOK.C: In this program, a loop uses strtok
 * to print all the tokens (separated by commas
 * or blanks) in the string named "string".
 */

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

char string[] = "A string\tof ,,tokens\nand some  more tokens";
char seps[]   = " ,\t\n";
char *token;

void main( void )
{
   printf( "%s\n\nTokens:\n", string );
   /* Establish string and get the first token: */
   token = strtok( string, seps );
   while( token != NULL )
   {
      /* While there are tokens in "string" */
      printf( " %s\n", token );
      /* Get next token: */
      token = strtok( NULL, seps );
   }
}

or

http://www.boost.org/libs/tokenizer/tokenizer.htm

Avatar of bcladd
bcladd

_Accelerated C++_ by Koenig and Moo has a good exmaple of a string splitter, too. Even without the book you can get their source code at http://www.acceleratedcpp.com/. It takes a string and splits it into a vector of strings, one entry per "word" for some definition of word (non-space between spaces).

I would suggest the book, too, since it is a good read.

-bcl
Avatar of meow00

ASKER

Hello,

   Thanks for the answers.... I just have one more question about jkr's answer. What if I have a string "string" as follows :
--------------------------------
#include <string>
#include <cstdio>
#include <iostream>
#include <fstream>

char seps[]   = " ,.!a\t\n";
char *token;
string meow = "I am the laziest cat in the world...meow " ;
char woof[] = " I am,a happy dog !" ;

void main( void )
{
//   token = strtok(meow.c_str(), seps) ;  //...line01
   token = strtok(woof,seps) ;
   while(token != NULL){
    cout << token << endl ;
    token = strtok(NULL, seps) ;
   }
}
---------------------------------------------
 line01 doesn't work now ......
 Is there anyway to make line01 work ? thanks !

 meow ......
~
SOLUTION
Avatar of bcladd
bcladd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Sure, show an easier way to accomplish the same thing.

<smile>

-bcl