Serialization/Unserialization of a list of strings - C++ Are there any issues with below implementation?
Input: from STDIN, a list of strings
Output: serialize to a file called "out.txt" and then unserialize into a list of strings, and output to STDOUT  

Goal is not here too much about code organization, but I would like to know if this code can fail for certain types of inputs  

#include &lt;iostream&gt;
#include &lt;fstream&gt;
#include &lt;sstream&gt;
#include &lt;vector&gt;
#include &lt;cstdlib&gt;

using namespace std;

int main()
{
    std::vector&lt;std::string&gt; input;

    std::string tmp;
    while (std::cin &gt;&gt; tmp)
    {
        input.push_back(tmp);
    }

    std::ofstream out("out.txt", std::ios::out);
    for (std::vector&lt;std::string&gt;::const_iterator iter = input.begin();
         iter != input.end();
         ++iter)
    {
        out &lt;&lt; (*iter).size() &lt;&lt; "|" &lt;&lt; *iter;
    }
    out.close();

    std::vector&lt;std::string&gt; output;
    std::ifstream in("out.txt", std::ios::in);
    if (in.is_open())
    {
        std::string tmp;
        if (!in.eof())
        {
            getline(in, tmp);
            size_t pos;
            int length = 0;
            std::string str;

            while (!tmp.empty())
            {
                pos = tmp.find('|');
                if (pos != std::string::npos)
                {
                    length = atoi(tmp.substr(0, pos).c_str());
                    str = tmp.substr(pos+1, length);     
                    tmp = tmp.substr(pos+str.size() + 1);

                    output.push_back(str);
                }
            }
        }

        in.close();
    }

    for (std::vector&lt;std::string&gt;::const_iterator iter = output.begin();
         iter != output.end();
         ++iter)
    {
        std::cout &lt;&lt; *iter &lt;&lt; "\n";
    }

    return 0;
}
Looks like it will work fine.
You preserve the string integrity in the file by maintaining its length as a separate entity that you can check independently. So it should work fine.

Not sure why you copy it to intermediate arrays.
Why not copy directly from input to file then from file to output?

You implement most of the other loops so precisely. But the main loop to read from the file is a bit a bit bulky and messey. With an extra class you can make it look just like the others.

std::vector&lt;std::string&gt; output;
std::ifstream in("out.txt", std::ios::in);
MyStringReader    reader;

while(in &gt;&gt; reader)
{
    output.push_back(reader);
}
in.close();


So now you just need to define the MyClassReader

struct MyStringReader
{
    std::string   data;
    operator std::string const&amp;() {return data;}  // This is used in the line
};                                                // output.push_back(reader) and converts
                                                  // the reader object into a string before
                                                  // it is pushed

std::istream&amp; operator&gt;&gt;(std::istream&amp; stream, MyStringReader&amp; value)
{
    size_t length;
    char   sep    = 'X';

    // Use the stream operators to get the size (and separator)
    // Much nicer than using tha atoi() function.
    //
    if ((stream &gt;&gt; length &gt;&gt; sep) &amp;&amp; (sep == '|'))
    {
        // Resize the string to the correct size and put the word into it.
        value.data.resize(length);
        stream.read(&amp;value.data[0], length);
    }
    return stream;
}