reading file

Hi,
Im looking for a way to read a sentence into a string and take out each word as separate strings. I dont even know where to start coding this.
#include <iostream>
#include <sstream>

using namespace std;

int main(void)
{
string sentence, word;
stringstream ss;

cout << "input a sentence plz:";

getline(cin,sentence);

ss << sentence;

while(ss >> word) cout << word << endl;

return 0;
}
well i was using fgets() to read the line. ur code is kind of confusing...

this is what I have thus far...

from in main I am using;

while(fgets(line,sizeof line,fp)!=NULL){
array[size]= buildTree(root,line);
size++;
}


and this is the function;


treeNodeptr buildTree(treeNodeptr root, char line[]){

char temp[51];
int s=0, c;


while(line[s]!='\n'){
c=0;

while(line[s] != '\n' && line[s]!=' '){
temp[c]=line[s];
c++;
s++;
}

temp[c]='\0';
s++;
treeNodeptr newn;
strcpy(newn->word, temp);
root= treeInsert(root,newn);
}
return root;
}
Last edited on
Topic archived. No new replies allowed.