Page MenuHomec4science

reader.cpp
No OneTemporary

File Metadata

Created
Sun, Nov 10, 17:37

reader.cpp

//
// Created by Arnaud Pannatier on 06.05.18.
//
#include "reader.h"
#include <fstream>
#include <iostream>
#include <limits>
Grid Reader::readGridFromFile (std::string filename,int nx, int 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(int x(0); x<nx;x++) {
for (int 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, int nx, int ny) {
std::cout << filename << std::endl;
double* double_values;
double_values= new double[nx*ny];
for(int x(0); x<nx;x++) {
for (int 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