You can use stream iterators to first read your input file into std::vector, saving each line:
using inliner = std::istream_iterator<Line>;
std::vector<Line> lines{
inliner(stream),
inliner()
};
With the structure of the Lineannouncement is necessary operator>>based on std::getline:
struct Line
{
std::string content;
friend std::istream& operator>>(std::istream& is, Line& line)
{
return std::getline(is, line.content);
}
};
Working example:
#include <iostream>
#include <iterator>
#include <sstream>
#include <vector>
struct Line
{
std::string content;
friend std::istream& operator>>(std::istream& is, Line& line)
{
return std::getline(is, line.content);
}
};
int main()
{
std::istringstream stream("one\ntwo\nthree");
using inliner = std::istream_iterator<Line>;
std::vector<Line> lines{
inliner(stream),
inliner()
};
for(auto& line : lines)
{
int i = 1;
std::cout << "<s> " << line.content << " </s> (" << i << ")" << std::endl;
}
}
To complete your work, change the line stream for the file stream and output the completed result to a file.
source
share