median-comparison/fileHandler.h

38 lines
1.1 KiB
C
Raw Permalink Normal View History

2020-10-06 14:34:16 +02:00
#pragma once
#include <string>
#include <iostream>
#include <fstream>
#include <vector>
2020-10-17 00:28:24 +02:00
std::vector<uint32_t > readFromFile(const char* filePath)
2020-10-06 14:34:16 +02:00
{
2020-10-17 00:28:24 +02:00
std::vector<uint32_t > numbers;
2020-10-17 23:22:17 +02:00
//std::cout << "read file: " << filePath << "..." << std::endl;
2020-10-06 14:34:16 +02:00
std::ifstream in(filePath);
if (in.is_open())
{
std::string line;
std::getline(in, line);
2020-10-17 00:28:24 +02:00
int count = std::stoi(line);
2020-10-17 23:22:17 +02:00
//std::cout << "total: " << count << " elems" << std::endl;
2020-10-06 14:34:16 +02:00
2020-10-17 00:28:24 +02:00
int idx = 0;
2020-10-06 14:34:16 +02:00
while (in.good() && idx < count) {
std::getline(in, line);
//long val = std::stol(line);
//std::cout << "as long: " << val << std::endl;
unsigned long ulval = std::stoul(line);
//std::cout << "as unsigned long: " << ulval << std::endl;
2020-10-17 00:28:24 +02:00
numbers.push_back((uint32_t)ulval); // auto cast to uint
2020-10-06 14:34:16 +02:00
idx++;
}
}
else std::cout << "Error opening " << filePath << std::endl;
if (!in.eof() && in.fail())
std::cout << "error reading " << filePath << std::endl;
in.close();
return numbers;
}