Can't read parameters from file

  • Thread starter Thread starter madtraveller
  • Start date Start date
  • Tags Tags
    File Parameters
Click For Summary

Discussion Overview

The discussion revolves around issues related to reading parameters from a text file in C++. Participants are exploring how to correctly implement file reading functions, handle different data formats, and manage variable names in their code. The scope includes programming challenges and code debugging.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their attempt to read parameters from a text file but finds that their function does not receive any parameters, presenting their code for review.
  • The same participant provides a second version of their code that works correctly, indicating that they manually set parameter values instead of reading from the file.
  • Another participant expresses confusion regarding the format of the input data, noting discrepancies between the described parameters and the actual data format in the file.
  • Concerns are raised about the clarity of the code due to the use of single-letter variable names, suggesting that this may hinder understanding.
  • A participant requests guidance on reading a more complex text file format, specifically how to handle spaces and different lengths of strings, and mentions using stringstream without success.
  • There is a request for help on how to open a file with a non-fixed filename in the context of the provided code.

Areas of Agreement / Disagreement

Participants do not appear to reach a consensus on the correct format for the input data or the best approach to read it. Multiple competing views on code clarity and functionality remain unresolved.

Contextual Notes

Participants highlight limitations in their understanding of file handling in C++, particularly regarding variable naming conventions and the syntax for reading files with variable names.

madtraveller
Messages
28
Reaction score
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
 
Physics news on Phys.org
madtraveller said:

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
I'm confused by your descriptions of the format of data in the input file. The things you list above don't seem to what you describe below. For example, above you have distance, velocity, and Diff_coeff (whatever that is), but below you have Ndata, Nplace, Date, Hour, Place 1, Place 2, etc.

What exactly is the format of data in the file? It would also be helpful to show a few of the lines of data as they appear in the file.

Also, your code is hard to follow, with so many single-letter variable names.


madtraveller said:
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.
 
Hi Mark44,
Thank you very much for your reply
Actually, as I wrote at the end of my post, I don't know how to read text file with space in between together with number as well as how to extract year, month and hour from that kind of format. So first, I created a text file with "_" connected word so that I can use one "dummystring" to read it. And hope that anyone can show me how to read a real input file afterward. My problem is that after read_parameter_from_file() executed, my recalculate() function still didn't receive any values from n, dx, dt, u and D

My first data file (with legend)

Number_of_points 80 // n
Distance 10.0 // dx
Time_step 0.05 // dt
Velocity 5.0 // u
Diff_coeff 50.0 // D

Here is the real input file

Number of data points 25
Number of places 6
Timestep 1
Date Hour Place1 Place2 Place3 Place4 Place5 Place6
1990-01-01 1 25.002 NA NA 16.265 6.231 9.680
1990-01-01 2 24.449 NA NA 16.265 6.231 9.551
1990-01-01 3 24.449 NA NA 16.265 6.231 9.551
1990-01-01 4 24.550 NA NA 16.265 6.231 9.551
1990-01-01 5 24.851 NA NA 16.265 6.130 9.551
1990-01-01 6 25.002 NA NA 16.099 6.130 9.421
1990-01-01 7 25.306 NA NA 15.933 6.130 9.421
1990-01-01 8 25.357 NA NA 15.933 6.130 9.421
1990-01-01 9 25.357 NA NA 15.933 6.029 9.389
1990-01-01 10 25.306 NA NA 15.769 6.029 9.260
1990-01-01 11 25.306 NA NA 15.769 6.029 9.260
1990-01-01 12 25.103 NA NA 15.605 6.029 9.132
1990-01-01 13 24.952 NA NA 15.605 6.029 9.132
1990-01-01 14 24.901 NA NA 15.605 5.905 9.132
1990-01-01 15 24.851 NA NA 15.442 6.004 9.132
1990-01-01 16 24.801 NA NA 15.442 5.905 9.003
1990-01-01 17 24.700 NA NA 15.442 5.905 9.003
1990-01-01 18 24.550 NA NA 15.442 5.905 9.003
1990-01-01 19 24.350 NA NA 15.442 5.905 9.003
1990-01-01 20 24.150 NA NA 15.279 5.905 9.003
1990-01-01 21 23.952 NA NA 15.239 5.806 8.875
1990-01-01 22 23.803 NA NA 15.078 5.806 8.875
1990-01-01 23 23.704 NA NA 14.758 5.806 8.875
1990-01-01 24 23.557 NA NA 14.758 5.806 8.875
1990-01-02 1 23.458 NA NA 14.758 5.684 8.875

The mathematical description of my function you can find in this post
https://www.physicsforums.com/showthread.php?t=430651

Thank you for your time

MT
 
Thank you, Mark44
Could you also show me the syntax to call a read and write function using anonymous file names.
E.g: in class
Code:
  void transfer::get_parameter_from_file(string &filename)

but the following code resulted in syntax errors

Code:
 ifstream inputFile;
inputFile.open (filename);
Code:
int main ()
{
    transfer transferfunction;

    transferfunction.get_parameter_from_file(filename);
  
    return 0;
}
 
It would be helpful to know what the errors are. If they are syntax errors, the compiler reports them and gives the line number at which they occur. Also, you don't show enough of your code for me to say for sure, but it looks like you are trying to pass variables that are undeclared or undefined.

In main, unless filename has been defined at global scope, the compiler won't know what to do with it.

Please include the compiler error messages. If there are a whole lot of them, just include the first five or so. Also, please include the current version of your code.
 
I found the correct syntax:

In my class, I should have
Code:
bool get_parameter_from_file(const string &filename);

In function outside class, the code should be

Code:
bool transfer::get_parameter_from_file(const string &filename) 
{
    ifstream inputFile(filename.c_str());
...

Then in main function
Code:
int main ()
{
   transfer calling;
   calling.get_parameter_from_file(string("whatever_file_name_is.txt"));
...
}

Thank you for your time

MT
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 2 ·
Replies
2
Views
5K
Replies
9
Views
2K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 4 ·
Replies
4
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 15 ·
Replies
15
Views
3K