C++: continuous reading file while doing calculation in between?

Click For Summary

Discussion Overview

The discussion revolves around a programming problem in C++ related to continuously reading parameters from a file while performing calculations. Participants explore how to manage file input and function design to achieve this functionality.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant describes their approach of reading parameters from a file within a function and expresses difficulty in reading subsequent lines after the first.
  • Another participant suggests passing the file stream as an argument to the function instead of the filename.
  • A later reply elaborates that the issue arises from opening and closing the file within the function, recommending that the stream should be created and managed outside the function.
  • One participant expresses gratitude and indicates that their issue was resolved after following the advice given.

Areas of Agreement / Disagreement

There is a general agreement on the approach of passing the stream as an argument, but the initial participant's understanding of the problem and the solution remains somewhat unclear, as indicated by their request for more detail.

Contextual Notes

The discussion does not delve into specific implementations or the broader implications of the proposed solutions, focusing instead on the immediate coding challenge.

madtraveller
Messages
28
Reaction score
0

Homework Statement


I have 1 class for reading parameter from file (example below) and 1 class for doing calculation
For every step, I'd like to read in parameter and do calculation in between.

For i = 0 to number of lines in parameter file
Read in parameter;
Calculation;
End

My problem is that I don't know how to read a file continuously. Every time I called the function in parameter class, it just received the first line :(

Could anyone give me some suggestion?
Thank you

* Sample parameter file
Code:
m	TD	T	V	Sa	Sb
0.5	246.6	39.6	6.1	0.4	0.6
0.5	394.8	39.7	4.6	0.9	0.2
0.0	391.3	39.0	3.6	1.0	0.6
0.4	350.1	39.0	8.8	0.6	0.3
0.3	94.1	38.9	6.0	0.6	0.7
0.5	95.0	38.5	7.2	1.0	0.0
0.3	86.4	39.3	3.4	0.4	0.4
0.5	404.5	39.8	0.9	0.9	0.3
0.4	252.7	39.3	7.9	0.6	0.4
0.5	1.3	39.9	2.4	1.0	0.7
0.2	36.0	39.9	3.5	0.4	0.5
...

The attempt at a solution

Parameter class:

Code:
bool parameters::get_parameter_from_file(const string &filename)
{
 
    ifstream inputfile(filename.c_str());

    if ( !inputfile )
    {
        cerr << "Error: file could not be opened" << endl;
        return false;
    }

    string dummy_string;                // temporary string to store unnecessary words

    // read through the header
    inputfile >> dummy_string >> dummy_string >> dummy_string
            >> dummy_string >> dummy_string >> dummy_string;

    // read parameters
    inputfile >> m >> TD >> T >> V >> Sa >> Sb;

   inputfile.close();
    return true;
}
 
Physics news on Phys.org
Pass the stream as an argument to get_parameter_from_file instead of the file name.
 
D H said:
Pass the stream as an argument to get_parameter_from_file instead of the file name.

Thanks. Maybe I'm just stupid but could you please be more detailed on your point?
 
The entire problem stems from your opening the file inside that function and then closing it before exiting. The solution is the same as the advice given by a doctor to a complaint, "Doc, it hurts when I do this." The advice: "Don't do that then."

What you need to do instead is to pass the stream as an argument to the function instead of creating the stream inside the function. That means you have to create (and destroy) the stream outside of your function. In your main(), for example.
 
Thanks DH. Mine works fine now!
 

Similar threads

  • · Replies 6 ·
Replies
6
Views
2K
  • · Replies 14 ·
Replies
14
Views
4K
  • · Replies 5 ·
Replies
5
Views
5K
  • · Replies 2 ·
Replies
2
Views
2K
  • · Replies 8 ·
Replies
8
Views
2K
Replies
3
Views
3K
Replies
7
Views
4K
  • · Replies 7 ·
Replies
7
Views
32K
  • · Replies 12 ·
Replies
12
Views
3K