- #1
madtraveller
- 28
- 0
Homework Statement
I'm trying to read parameters from a text file (format below) but actually my function didn't receive any parameters from read file function.
Number_of_points 80
Distance 10.0
Time_step 0.05
Velocity 5.0
Diff_coeff 50.0
Homework Equations
The Attempt at a Solution
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
class transfer {
private:
int n;
double dx, dt, u, D;
double* trans;
public:
transfer();
bool get_parameter_from_file();
void recalculate();
bool write_value_to_file();
};
transfer::transfer() {
n = 0;
dx = 0;
dt = 0;
u = 0;
D = 0;
}
bool transfer::get_parameter_from_file() {
ifstream inputFile;
inputFile.open ("parameter.txt");
if ( !inputFile ) { // check if file couldn't be opened
cerr << "Error: file could not be opened" << endl;
return false;
}
string dummy_string;
inputFile >> dummy_string >> n;
cout << dummy_string << " " << n << endl;
inputFile >> dummy_string >> dx;
cout << dummy_string << " " << dx << endl;
inputFile >> dummy_string >> dt;
inputFile >> dummy_string >> D;
inputFile >> dummy_string >> u;
cout << dummy_string << " " << u << endl;
inputFile.close();
return true;
}
void transfer::recalculate() {
delete[] trans;
trans = new double[ n ];
const double PI = 3.14159;
double c;
double t;
c = 5.0/3.0 * u;
trans[0] = 0.0;
for (int i=1; i < n; ++i) {
double diffuse;
t = dt * i;
trans[i] = dx / ( 2.0 * sqrt( PI * D * pow(t , 3)));
diffuse = dx - c * t;
diffuse = exp ( - pow (diffuse, 2) / ( 4.0 * D * t ));
trans[i] = trans[i] * diffuse;
cout << setw(3) << t << setw(18) << trans[i] << endl;
}
}
bool transfer::write_value_to_file() {
// Creat a file to store the result
ofstream outdata;
outdata.open ("0.result.txt", ios::out);
if ( !outdata ) { // check if file couldn't be opened
cerr << "Error: file could not be opened" << endl;
return false;
}
// Write result to file
outdata << "Time" << setw (21) << "Transfer function" << endl;
for (int i = 0; i < n; ++i) {
outdata << setw(3) << i << setw(18) << trans[i] << endl;
}
outdata.close();
return true;
}
// Main program
int main ()
{
transfer transferfunction;
transferfunction.recalculate();
transferfunction.write_value_to_file();
// Terminate program
return 0;
}
If I did it like this and my program worked fine
Code:
#include <iostream>
#include <fstream>
#include <cmath>
#include <iomanip>
#include <cstdlib>
#include <string>
using namespace std;
class transfer {
private:
int n;
double dx, dt, u, D;
double* trans;
public:
transfer();
bool set_n_value(const int &new_n);
int get_n_value();
void set_dx_value(double new_dx);
double get_dx_value();
void set_dt_value(double new_dt);
double get_dt_value();
void set_u_value(double new_u);
double get_u_value();
void set_D_value(double new_D);
double get_D_value();
void recalculate();
bool write_value_to_file();
};
transfer::transfer() {
n = 0;
dx = 0;
dt = 0;
u = 0;
D = 0;
}
bool transfer::set_n_value(const int &new_n) {
if ( n < 0 ) {
cerr << "Error: non-physical meaning parameter" << endl;
return false;
}
n = new_n;
return true;
}
int transfer::get_n_value() {
return n;
}
void transfer::set_dx_value(double new_dx) {
dx = new_dx;
}
double transfer::get_dx_value() {
return dx;
}
void transfer::set_dt_value(double new_dt) {
dt = new_dt;
}
double transfer::get_dt_value() {
return dt;
}
void transfer::set_u_value(double new_u) {
u = new_u;
}
double transfer::get_u_value() {
return u;
}
void transfer::set_D_value(double new_D) {
D = new_D;
}
double transfer::get_D_value() {
return D;
}
void transfer::recalculate() {
delete[] trans;
trans = new double[ n ];
const double PI = 3.14159;
double c;
double t;
c = 5.0/3.0 * u;
trans[0] = 0.0;
for (int i=1; i < n; ++i) {
double diffuse;
t = dt * i;
trans[i] = dx / ( 2.0 * sqrt( PI * D * pow(t , 3)));
diffuse = dx - c * t;
diffuse = exp ( - pow (diffuse, 2) / ( 4.0 * D * t ));
trans[i] = trans[i] * diffuse;
cout << setw(3) << t << setw(18) << trans[i] << endl;
}
}
bool transfer::write_value_to_file() {
// Creat a file to store the result
ofstream outdata;
outdata.open ("0.result.txt", ios::out);
if ( !outdata ) { // check if file couldn't be opened
cerr << "Error: file could not be opened" << endl;
return false;
}
// Write result to file
outdata << "Time" << setw (21) << "Transfer function" << endl;
for (int i = 0; i < n; ++i) {
outdata << setw(3) << i << setw(18) << trans[i] << endl;
}
outdata.close();
return true;
}
// Main program
int main ()
{
transfer transferfunction;
transferfunction.set_n_value(80);
transferfunction.get_n_value();
transferfunction.set_dx_value(10.0);
transferfunction.get_dx_value();
transferfunction.set_dt_value(0.05);
transferfunction.get_dt_value();
transferfunction.set_u_value(5.0);
transferfunction.get_u_value();
transferfunction.set_D_value(50.0);
transferfunction.get_D_value();
transferfunction.recalculate();
transferfunction.write_value_to_file();
// Terminate program
return 0;
}
Btw, Could anybody explain to me how to read this type of text file (e.g handle space between words, separate year, month and date...). I tried to use stringstream but I don't know how to handle strings with different length. Example file is below
Number of point Ndata
Number of place Nplace
timestep dt
Date Hour Place 1 Place 2 ...
2010-01-01 1 a1 a2
...
2010-01-01 24 a24 a24
...
And actually, I'd like to call a read and write function using non-fixed file name.
Code:
void transfer::get_parameter_from_file(string &filename)
But I don't know the syntax for open file as well as call it in main function
Code:
ifstream inputFile;
inputFile.open (filename);
Plz help me with this. Thank you
Kind regards,
MT