It's not clear to me what you're trying to do. If the entire
contents of the file are "kyu", then the input definitely
will stop when it reaches end of file. What I suspect (but I'm
really just guessing) is that you've output the strings without
a separator, and so the input doesn't know where each string
ends, and reads all of the characters as a single string. In
other words, your output file contains "junhuakyu...".
When outputting strings (and a lot of other things) as text, you
must find some way of deliminating them. If the strings use
a restricted character set (e.g. just letters), it's often
sufficient to just insert a separator character between each
string, choosing one which cannot appear in the strings. For
example:
for ( auto p = std::begin(name); p != std::end(name); ++ p ) {
if ( p != std::begin(name) ) {
std::cout << ' ';
}
std::cout << *p;
}
This will give you something like "jun hua kyu ..." in the
file, which can easily be read using:
std::string st;
while ( std::cin >> st ) {
// ...
}
Which brings up a secont problem with your code. You're using
the results of what you've read without testing whether the read
succeeded. If it fails (e.g. because of end of file), then st
may not have been modified; at any rate, if the file is a text
file, and really only contains text, you'll never get st ==
"\0"; this can only occur if the file actually contains
a '\0' character (which isn't legal if it is a text file).
Finally, of course: the choice of separator is up to you. White
space is particularly simple; if the strings can contain blanks,
you might choose '\n', and use std::getline to read. And if
worse comes to worse, and the strings can be literally anything,
you'll have to implement some sort of quoting mechanism,
possibly with escape characters.
std::vectorthe string that I input doesn't stopWhat do you mean?kyu, nothuaoutput: huaNo, output: kyustd::cinthe strings you write instd::coutin the same program?