i have problems with an ifstream. I want to split the ifstream in n parts.
For example n = 3:
- Ifstream includes first 1/3 of the file.
- Ifstream includes second 1/3 of the file.
- Ifstream includes third 1/3 of the file.
std::ifstream in("test.txt");
std::vector<std::string> v1;
std::vector<std::string> v2;
std::vector<std::string> v3;
//first 1/3 of file
read(in, v1);
//second 1/3 of file
read(in, v2);
//third 1/3 of file
read(in, v3);
read(in, v){
std::string line {""};
while(getline(in, line)){
v.pushback(line);
}
}
You can read and push all lines in a vector and then split the vector in 3 part, for example:
And well now if you wanna do this for n partitions you can do something like that:
Where
vChunksthe first n partitions have the number of lines between n dimensions and in n + 1 has dimension the rest of the last lines if it is not divisible by n the total number of lines