ENERGY MONITOR 0.2
Loading...
Searching...
No Matches
data_IO.cpp
Go to the documentation of this file.
1#include "data_IO.h"
2
3#include <string>
4#include <iostream>
5#include <iomanip>
6
8
9bool file_IO::csv_import(string file_address, v_container &table){
10 ifstream file;
11 string cell;
12 char c;
13
14 table.get_row_num();
15
16 int row_index = 0;
17 int col_index = 0;
18 file.open(file_address);
19 if(file.is_open()){
20 //operations to fill the v_container object from the file
21 while(file.get(c)){
22
23 if(c == '\n' || c == ','){
24
25 try{
26 table.insertData(stod(cell), row_index, col_index);
27 cell = "";
28 }
29 catch(const std::invalid_argument& e){
30 std::cerr << "FAILED TO READ CORRUPTED FILE "<< e.what() << endl;
31 return false;
32 }
33 catch(const std::out_of_range& e){
34 std::cerr << "VERY LARGE OR VERY SMALL values IN THE FILE "<< e.what() << endl;
35 return false;
36 }
37
38 if(c == '\n'){
39 col_index = 0;
40 row_index++;
41 }else{
42 col_index++;
43 }
44 }else
45 {
46 cell.push_back(c);
47 }
48 }
49 file.close();
50 return true;
51 }
52 else{
53 return false;
54 }
55}
56
57
58
59
60bool file_IO::csv_export(string file_address, v_container &table){
61 ofstream outputFile;
62 outputFile << std::setprecision(10);
63 outputFile.open(file_address);
64 if(!outputFile.is_open()){
65 cout << "unexpected error" << endl;
66 return false;
67 }else{
68
69 unsigned int col_index;
70 unsigned int row_index;
71 for(row_index = 0; row_index < table.get_row_num(); row_index++){
72 for(col_index = 0; col_index < (table.get_col_num() - 1); col_index++){
73 outputFile << table.getData(row_index,col_index) << ',';
74 }
75 outputFile << table.getData(row_index,col_index) << endl;
76 }
77 outputFile.close();
78 return true;
79 }
80};
81
82
83
84bool file_IO::data_import(string file_address, v_container &data, int type){
85 switch (type)
86 {
87 case csv:
88 return csv_import(file_address, data);
89 break;
90 default:
91 return false;
92 break;
93 }
94}
95
96bool file_IO::data_export(string file_address, v_container &data, int type){
97 switch (type)
98 {
99 case csv:
100 return csv_export(file_address, data);
101 break;
102 default:
103 return false;
104 break;
105 }
106}
HELD_DATA getData(int row, int col) const
return the data at some address
Definition dataTable.h:64
void insertData(HELD_DATA d, unsigned int row, unsigned int col)
inserts a value to a cell of dataTable instance with that address
Definition dataTable.h:46
unsigned int get_col_num()
Definition dataTable.h:41
unsigned int get_row_num()
Definition dataTable.h:38
contains a basic flexible and extensible interface for file imports and exports operations other stru...
@ csv
Definition data_IO.h:23
bool data_import(string file_address, dataTable< double > &data, int type)
a function that imports files with supported file formats
Definition data_IO.cpp:84
bool data_export(string file_address, dataTable< double > &data, int type)
Definition data_IO.cpp:96