Can't read parameters from file

In summary, the program attempted to set the value of n to 80 but failed to do so. It then attempted to set the values of dx, dt, u, and D but all failed.
  • #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
 
Physics news on Phys.org
  • #2
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.
 
  • #3
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
 
  • #5
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;
}
 
  • #6
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.
 
  • #7
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
 

Related to Can't read parameters from file

1. Why am I getting a "Can't read parameters from file" error message?

There could be several reasons for this error message. It is possible that the file containing the parameters is not in the correct format or is corrupted. Another possibility is that the file path is incorrect or the file is not accessible due to permissions. It is also possible that there is an issue with the code that is trying to read the parameters from the file.

2. How can I fix the "Can't read parameters from file" error?

To fix this error, you will need to troubleshoot the possible causes mentioned above. First, make sure that the file containing the parameters is in the correct format and is not corrupted. Then, check the file path and ensure that the file is accessible. If the issue persists, review the code that is trying to read the parameters and check for any errors.

3. Can a missing file cause the "Can't read parameters from file" error?

Yes, a missing file can cause this error. If the file containing the parameters is not present in the specified file path, it will result in the "Can't read parameters from file" error. Make sure that the file is present in the correct location before attempting to read the parameters from it.

4. Is there a specific file format for the parameters file to avoid the "Can't read parameters from file" error?

Yes, there is a specific file format that is commonly used for parameters files, such as JSON, XML, or CSV. However, the file format may vary depending on the programming language or software being used. It is important to check the documentation or guidelines for the specific file format that is required for the parameters file.

5. Can a permissions issue cause the "Can't read parameters from file" error?

Yes, a permissions issue can cause this error if the file containing the parameters is not accessible due to restricted permissions. Make sure that the file has the necessary permissions for the code to read the parameters from it. If you are not sure about the permissions, consult the system administrator or the documentation for the programming language or software being used.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
2
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
891
  • Engineering and Comp Sci Homework Help
Replies
2
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
871
  • Engineering and Comp Sci Homework Help
Replies
2
Views
1K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
8
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
911
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top