Page MenuHomec4science

reader.cpp
No OneTemporary

File Metadata

Created
Fri, Nov 8, 00:28

reader.cpp

//
// Created by Arnaud Pannatier on 06.05.18.
//
#include "reader.h"
#include <fstream>
#include <iostream>
Grid Reader::readGridFromFile (std::string filename,std::size_t nx, std::size_t ny) {
Grid ret = Grid(nx,ny);
std::cout << filename << std::endl;
std::ifstream file (filename, std::ios::in | std::ios::binary);
if (file.is_open())
{
char * memblock;
file.ignore( std::numeric_limits<std::streamsize>::max() );
std::streamsize size = file.gcount();
file.clear();
std::cout << size << " -- size " << std::endl;
memblock = new char[size];
file.seekg (0, std::ios::beg);
file.read (memblock, size);
file.close();
//std::cout << "the entire file content is in memory" << std::endl;
double* double_values = reinterpret_cast<double*>(memblock);
for(std::size_t x(0); x<nx;x++) {
for (std::size_t y (0); y < ny; y++) {
ret (x, y) = double_values[y * nx + x];
}
}
delete memblock;
}else{
std::cout<< "File is not open ! " << std::endl;
}
return ret;
}
void Reader::writeGridInFile (Grid& g,std::string filename, std::size_t nx, std::size_t ny) {
std::cout << filename << std::endl;
double* double_values;
double_values= new double[nx*ny];
for(std::size_t x(0); x<nx;x++) {
for (std::size_t y (0); y < ny; y++) {
double_values[x * nx + y] = g(y,x);
}
}
char* memblock = reinterpret_cast<char*>(double_values);
std::ofstream file(filename, std::ios::out | std::ios::binary);
file.write (memblock, sizeof(memblock)*nx*ny);
std::cout << "Grid Saved ! " << std::endl;
delete double_values;
}

Event Timeline