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 <iostream>
#include <fstream>
#include <sstream>
#include <vector>
#include <cstdlib>
using namespace std;
int main()
{
std::vector<std::string> input;
std::string tmp;
while (std::cin >> tmp)
{
input.push_back(tmp);
}
std::ofstream out("out.txt", std::ios::out);
for (std::vector<std::string>::const_iterator iter = input.begin();
iter != input.end();
++iter)
{
out << (*iter).size() << "|" << *iter;
}
out.close();
std::vector<std::string> 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<std::string>::const_iterator iter = output.begin();
iter != output.end();
++iter)
{
std::cout << *iter << "\n";
}
return 0;
}