Read series data from txt file

Dear forums members',

I am really beginner, and just registered in this forum 10 minutes a go. I got C++ code from Japanese professor, and I want to modify this code for my study purpose. The first step I want to modifying this code from static to little bit dynamic. For instance, determine gas pressure in tank by using ideal gas equation (P=nRT/V). If we assumed amount gas and volume tank are fixed for whole year, the daily gas pressure should be depend on atmospheric temperature.

I will very grateful if some one wishing teach me how to write syntax to read daily atmospheric temperature up to 10.000 days (for example) from txt file.

Best wishes,

Edi
Last edited on
see http://www.cplusplus.com/doc/tutorial/files/
then use a loop to read the numbers and an array or vector to store them.
Dear Hamsterman,

Thank a lot for your reply. Since I just beginner, it was difficult for me to understand what you explain about. As additional information, I saved data in atm_temp.txt consist of two column like below:
Time Temp.
(day) (K)
1 281.75
2 280.15
3 273.05
4 269.25
. .
. .
. .
999 290.05
1000 291.00

I will grateful if you wish to explain more detail.

Best wishes,

Edi
To read from a file, you'll probably want to use an ifstream or fstream, as described in the link hamsterman gave you.

To store the data, I'd suggest using reading the data into a temporary variable designed to hold your data, and then push_/*it*/back() into a vector, like so:

1
2
3
4
5
6
7
8
9
#include <vector>
/*Skip a few lines...*/
std::vector<data_type> data;
data_type temp;
while(open_ifstream.good())
{
    open_ifstream >> temp;
    data.push_back(temp);
}


EDIT: That looks a bit better.

-Albatross
Last edited on
Topic archived. No new replies allowed.