c++ - How does ifstream know where is the delimeter -
so when readfile("animal.txt") gave me
zebra baboon orangutan gorilla aardvark lion tiger cougar ocelot panther rat mouse gerbil hamster elephant rhinoceros hippopotamus
i know how ist >> s
identify delimiter , separate long string individual words. provided txt , implementation below .
animal.txt zebrababoonorangutangorillaaardvarkliontigercougarocelotpantherratmousegerbilhamsterelephantrhinoceroshippopotamus
and
sortedlist readfile(string infile) { sortedlist result; string s; ifstream ist(infile.c_str()); // open file // check if file opened correctly if(ist.fail()) throw runtime_error("file not found"); // read file list while(ist >> s){ cout<< s << endl; cout << ist << endl; result.insert(s); } return result; }
the operator>>
when applied stream on left , string on right.
will read "white space" separated word stream string.
mote precisely will:
- read , ignore characters until
issapce()
false. - read , store in string characters until
isspace()
true.
Comments
Post a Comment