'How can I read lines in vector<T> like ifstream
In the following code it works correctly:
int main(int argc, char *argv[]) {
uint64_t bucket_size = (kMemoryAvailable / sizeof(double) - sizeof(std::vector < double > )) * 0.7;
std::ifstream input_file(input_file_path);
size_t number_of_buckets = 0;
while (input_file) {
WriteBucket(input_file, bucket_size, number_of_buckets);
++number_of_buckets;
}
std::cout << "number_of_buckets: " << number_of_buckets << std::endl;
std::vector <std::ifstream> intermediate_outputs;
for (size_t i = 0; i < number_of_buckets; ++i) {
intermediate_outputs.push_back(std::ifstream("intermediate_output_" + std::to_string(i)));
}
std::ofstream output_file(output_file_path);
MergeFiles(intermediate_outputs, output_file);
output_file.close();
for (size_t i = 0; i < number_of_buckets; ++i) {
intermediate_outputs[i].close();
remove(("intermediate_output_" + std::to_string(i)).c_str());
}
}
I want to execute this code for any type as this code, but the file is not read and entered into the loop
template<typename T>
static void sorting(std::vector<T> &input_file) {
const uint64_t kMemoryAvailable = 100 * 1024 * 1024;
uint64_t bucket_size = (kMemoryAvailable / sizeof(double) - sizeof(std::vector<double>)) * 0.7;
size_t number_of_chunks = 0;
string output_file_path;
while (!input_file.empty()) {
WriteBucket(input_file, bucket_size, number_of_chunks);
++number_of_chunks;
}
vector<ifstream> intermediate_outputs;
for (size_t i = 0; i < number_of_chunks; ++i) {
intermediate_outputs.push_back(ifstream("intermediate_output_" + to_string(i)));
}
ofstream output_file(output_file_path);
MergeFiles(intermediate_outputs, output_file);
output_file.close();
for (size_t i = 0; i < number_of_chunks; ++i) {
intermediate_outputs[i].close();
remove(("intermediate_output_" + to_string(i)).c_str());
}
}
In this code I would like to read the input_file from the first code in the WriteBucket method to produce several intermediate_outputs that I will write on later, but this does not work in T .
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
