shorting data in C++

I have a text file with 100 years of data (1900 - 2000). each year is having 365 or 366 entry depending on leap year or not. I want to store the data of each year in separate files. the data in the main file is like this,

dd mm yy data1


please help me to write a C++ program to short the data¬¬
To... short the data? Sorry?
If the data is small enough to fit in memory, read it into a container, sort it by year and for each year, create a file with a suitable name, write all records, close file.
i am using the IF to short the data, but it is using only the first year, then stopping
What about showing us some code, with the code tags maybe?

(I'm explaining as I see you only have 2 posts)
I mean, you will find a button on the right like: <>

Press it, you'll see a [code ][/code] in your text. Put your code inside these tags like:

[code ]int a = 0;[/code] (Without the space)
and it will become
int a = 0;
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 #include<iostream>
#include<fstream>
using namespace std;

void main()
{
	int d,m,y,j;
	int a;
	float maxt,mint, prep, d1,d2,d3,d4,d5;

	ifstream weather("data.wth");
	ofstream month("1990.wth");
    
	while(weather>>d>>m>>y>>j>>maxt>>mint>>prep>>d1>>d2>>d3>>d4>>d5)
	{
		if(m==1990)                                  
			month<<d<<'\t'<<m<<'\t'<<y<<endl;
		else
			break;						//its not working
			
	}
	system("pause");
}
With statement
 
weather>>d>>m>>y>>j>>maxt>>mint>>prep>>d1>>d2>>d3>>d4>>d5
does your file have all of those fields on every line?

Should:
 
if(m==1990)
be:
 
if(y==1990)
?
Last edited on
line 16 should be if((m>=1990) && (m < 2000))
@kbw
Yes I have all the fields in every line.
you are right!! it should be y. After changing it to y, it is giving the output for the year 1990 but when I am changing it to 1991/other (for the dataset of the year 1991/other years), it is not working!!!

@coder777
this is giving the output from 1990 - 2000, it is the same as in the main (data.wth) file.
but I want separate file for each year!!
At the moment, you have code that reads the file and writes all the 1990 data to the 1990 data file. So you need to think of a way of expanding that for all years.

You can't open a file for every year at the same time, you just have too many. So you can't open lots of files and use an if or switch statement to select which file you write to. You have to think of another way.

If you're stuck, take a look at my first post.
how can I get that!!!
ya I understand that one!! but how can I short the data by year as you told. I think it is not possible by IF. please suggest how can modify the code!!!
Oh, you mean SORT! Not short! Ok, let me explain you. Create a string. Append your Current Year number. Create a file named as the string. Save Data.
Topic archived. No new replies allowed.