   Strange output from ofstream
  I am tokenizing a string and sending it to a ofstream but I am getting strange results.The \n sequence isn't working; it doesn't go to a new line.I'm getting Chinese/Japanese characters in the file.I think this may be due to compiling the project with the unicode character set rather than multibyte character set, but that's a guess.Here's my code:void Tokenize(std::string str, std::vector<std::string>& vs){    std::string s;    std::stringstream ss(str);    vs.clear();    while(ss >> s) vs.push_back(s);}  //... std::ofstream osp("prt.txt");std::string line = "begin particle \"name\"";std::vector<std::string> tokens;Tokenize(line, tokens);if(!tokens.empty()){    if(tokens[0] == "begin")    {   if(tokens[1] == "particle")   {  char open = tokens[2][0];  char close = tokens[tokens.size()-1][tokens[tokens.size()-1].length()];  osp << open << " " << tokens[2] << " " << close << tokens[tokens.size()-1] << "\n";   }    }}
  The problem is in this line: char close = tokens[tokens.size()-1][tokens[tokens.size()-1].length()];Strictly speaking, accessing somestring[somestring.length()] is a bogus call; you're reading past the end of the string. In this case you get lucky because the string internally has a NULL terminator, so your close variable is a NULL byte. When you go to output this to the file, oddness results.Indexing to length - 1 should solve the problem. However, your string parsing looks a bit dubious, because the resulting output to the file is probably not what you expect. But one fix at a time [smile]Also note that instead of tokens[tokens.size()-1] you can simply do tokens.back().   ;  Ahh thank you.What do you mean dubious? I got the output I expected, which was " "name" " "name"Why did the null terminator screw up all the previous stuff?I thought that when you write to a stream it adds it all to a buffer then prints the buffer when you flush the stream.I'm not sure how to buffer stores everything so I could be completely wrong about everything but I'd guess as chars. So wouldn't it just look like this: "string\0\0" (an extra null terminator), that printed fine when I tested it so I'm confused why mine had such adverse results.Please correct me (:On a side note:In my never ending quest to use all that C++ offers I decided to try string iterators but I get a runtime error that I don't understand. "string iterator not dereferencable" when I pretty much copied the example.std::string::iterator open = tokens[2].begin();std::string::iterator close = tokens.back().end();osp << *open << " " << *close << "\n";;  It means exactly what it says: a .end() iterator cannot be dereferenced, because it does not iterate to (? we need a better way of saying that) any actual element of the container, but instead to one past the end. This is so you can do nice and easily-understood things like:std::distance(container.begin(), container.end()); // is always equal to container.size()for (it = container.begin(), end = container.end(); it != end; ++it) {  // reaches every element in the container, even though the loop does not  // run when it == end}Unfortunately, std::string does not offer .front() and .back() like std::vector. However, you can use a reverse_iterator to the "reverse beginning" of the string to get an iterator to the last character.osp << *(tokens[2].begin()) << " " << *(tokens.back().rbegin()) << "\n";;  Oh yeaah, I get it now.  Thank you.   