Add way to extract numbers from the csv file

This commit is contained in:
2025-03-17 13:47:58 +01:00
parent fd03f5561f
commit 842e015c14
8 changed files with 103 additions and 6 deletions
+16
View File
@@ -0,0 +1,16 @@
#include "../Include/FileHelper.h"
#include <sys/stat.h>
FileHelper::FileHelper() {};
FileHelper::~FileHelper() {};
bool FileHelper::FileExists(char *path){
struct stat s;
// Check if file exists, and if it isn't a folder.
if (stat(path, &s) == 0 && !(s.st_mode & S_IFDIR)){
return true;
}
return false;
}
+12 -3
View File
@@ -1,9 +1,18 @@
#include "../Include/FileReader.h"
#include <iostream>
#include <string>
#include <fstream>
FileReader::FileReader() {};
FileReader::~FileReader() {};
char *FileReader::Read(){
return nullptr;
std::string FileReader::GetFile(char *path){
std::ifstream file(path);
std::string line;
std::getline(file, line);
file.close();
return line;
}
+19
View File
@@ -0,0 +1,19 @@
#include "../Include/NumberHelper.h"
#include <string>
#include <sstream>
#include <vector>
NumberHelper::NumberHelper() {};
NumberHelper::~NumberHelper() {};
std::vector<int> NumberHelper::GetNumbers(const std::string csv){
std::vector<int> rolls;
std::stringstream ss(csv);
std::string number;
while (std::getline(ss, number, ',')) {
rolls.push_back(std::stoi(number));
}
return rolls;
}
+22 -2
View File
@@ -1,7 +1,27 @@
#include <iostream>
#include <string>
#include "../Include/FileReader.h"
#include "../Include/FileHelper.h"
#include "../Include/NumberHelper.h"
int main() {
std::cout << "Hello, World!" << std::endl;
int main(int argc, char *argv[]) {
if(argc < 2) {
std::cerr << "Please provide a CSV formatted file.";
return 0;
}
if(!FileHelper::FileExists(argv[1])) {
std::cerr << "Filepath: " << argv[1] << " doesn't exist.";
return 0;
}
std::string file = FileReader::GetFile(argv[1]);
std::vector<int> numbers = NumberHelper::GetNumbers(file);
for(int i = 0; i < numbers.size(); i++) {
std::cout << numbers[i] << ' ';
}
return 0;
}