0

I know I've asked but I'm now faced with a different problem. The last implementation went wrong since the number of chars on a given line is u known.

ie

John 15 nmmo short brown hair look young
Andrew 22 young long bloack wavey hair with blue eyes.

etc...

I've tried getting fstream and string an d do:

file >> name >> age >> sname >> colour ....

but the problem is there isnt a set amound of words on a given line. So I thought of reading 1 char at a time

char ch = ' a'; // inistilise ch.

while((ch = file.get()) != EOF)

reads one ch at a time, my problem is storing the different chars as strings using the string class. but making sure ive got everything on that line in different strings.

How do I tell it when its hit a white space thats the end of 1 string start the next unless its a '\n' . If its a '\n' then call a function.

Cheers guys...

2
Contributors
1
Reply
2
Views
11 Years
Discussion Span
Last Post by Ancient Dragon
0

I would use getline() to read the entire line, then call string's find() method to locate the spaces.

std::string line;
while( getline(file,line) )
{
   while( line.length() > 0)
   {
      std::string word;
      int pos = line.find(' ');
      if(pos > 0)
      {
          word = line.substr(0,pos);
          line = line.substr(pos+1);
      }
      else
      {
          word = line;
           line = "";
      }
       do_something_with_word(word);
   }
}
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.