structure of a text file, which is supposed to be readed

Hi,

Hi I have a text File with a lot of data, which I am supposed to use as an input in a C++ program. There are several thousands observations and each observation is a line. The data is for a time period for a few years.

Now I have a code which reads all the data and makes calculations based on the all data.
NI have to make the same calculation but for shorter periods (e.g. instead of for the whole period I have to do calculations for every mounth to sum up all calculations and to find the mean and to see what is the deviation from the value for the whole period)

Now I have a problem with reading data form the text file for shorter periods... How can make the file suitbale for calculations for shorter periods? In the current version of the file I do not have any indications where a period is ending and a new one starting. How can I make the indications?

Regarding the code I was thinking to use fstream with getline().
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
using namespace std;

int main () {
  string line;
  ifstream myfile ("textfile.txt"); //defines the lokation of the file
  if (myfile.is_open())
  {
    while (! myfile.eof() ) // here is the indication until the end of the file, but I want to put it in earliar stage 
    {
      //do the calculations
//save each calculation
    }
    myfile.close();
  }

  return 0;
}


In this context how can I indicate in the text file to which line the data should be readed? Or may be there is a better approach?

10x!
Last edited on
Hi,

May be I will be able to get the txt file in the following form:

day 1
observation1
obsevation 2
:
:
day 2
observation1
obsevation 2
:
:
day i
observation1
obsevation 2
:
:

Now if I want to make my calculations using the observations for each day instead for the whole period how is the best way to start?

I found in google only info how to read the file to the end (by using EOF) but unfotinately I can not find out how to read it in peaces

10x for each post which can help me to start.
Would it be possible to edit the file by adding sentinel separators at the end of each day or month or year? Then you could read up to the separator
Topic archived. No new replies allowed.