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

In summary, the conversation is about a person having trouble reading a file continuously in their code. They are using two classes, one for reading parameters and one for calculation, and want to be able to read in a parameter for each step of the calculation. The problem stems from opening and closing the file within the function, so the solution is to pass the stream as an argument to the function instead of creating it inside.
  • #1
madtraveller
28
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
  • #2
Pass the stream as an argument to get_parameter_from_file instead of the file name.
 
  • #3
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?
 
  • #4
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.
 
  • #5
Thanks DH. Mine works fine now!
 

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

1. How can I continuously read a file while performing calculations in C++?

To continuously read a file while doing calculations in C++, you can use a combination of the while loop and ifstream object. The while loop will keep reading the file until it reaches the end, while the ifstream object allows you to read the file line by line.

2. What is the best way to handle errors while reading a file in C++?

The best way to handle errors while reading a file in C++ is to use the try-catch block. This allows you to catch any exceptions that may occur during the file reading process and handle them accordingly.

3. Can I perform calculations on the data from a file in real-time using C++?

Yes, you can perform calculations on the data from a file in real-time using C++. By continuously reading the file and performing calculations in between, you can get real-time results based on the data in the file.

4. How can I optimize the performance of reading a file and performing calculations in C++?

To optimize the performance of reading a file and performing calculations in C++, you can use techniques like buffering and parallel processing. Buffering allows you to read and store chunks of data from the file, while parallel processing allows you to perform calculations simultaneously on different parts of the data.

5. Is there a limit to the size of the file that can be continuously read and processed in C++?

The size of the file that can be continuously read and processed in C++ depends on the memory available on your system. If the file is too large to be stored in memory, you can use techniques like memory mapping to read and process the file in smaller chunks.

Similar threads

Back
Top