Hello,
I am trying to compile this program, however it is giving error
Error 2086. Infile Redefinition. Am I allowed to redefine Infile, or I am supposed to use it only once?

It might be silly error, Kindly help...
Thnaks in advance...

#include<fstream.h>
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#include<string.h>
#define pi 3.141593


struct met
{
	int hour;
	double AT1, AT2, RH1, RH2, Delta_t, Delta_rh;
};

struct pressure
{
	int hour;
	double p;
};

struct coll
{
	int hour;
	double jd,AT1, AT2,RH1, RH2, Delta_t, Delta_rh,p,SH1,SH2;  
};

void main()
{
	int i=0,j=0,k=0;

	struct met *a;

	struct pressure *c;

	struct coll *d;

	int noA=469,noC=721, noD=469;

	a=(struct met*)calloc(noA,sizeof(struct met));

	c=(struct pressure*)calloc(noC,sizeof(struct pressure));

	d=(struct coll*)calloc(noD,sizeof(struct coll));

	char str[400];

	ifstream infile("D:\\BIT Ranchi\\Temperature\\April 2008\\April_2008_avg.txt");

	while(!infile.eof())
	{
		if(i<2)
		{
			infile.getline(str, 400);
		}
		else
		{
			infile>>a[j].hour>>a[j].AT1>>a[j].AT2>>a[j].RH1>>a[j].RH2>>a[j].Delta_t>>a[j].Delta_rh;
			j++;
		}
		i++;
	}

	infile.close();

	char filename[]="D:\\BIT Ranchi\\Temperature\\April 2008\\April_pressure_mod1.txt";

	
	ifstream infile(filename);

	i=0;

	while(!infile.eof())
	{
		if(i<2)
		{
			infile.getline(str, 400);
		}
		else
		{
			infile>>c[k].p>>c[k].hour;
			k++;
		}
		i++;
	}

	infile.close();

	int g, h,l=0;

	double e;


	for(g=0;g<j;g++)
	{
		for(h=0;h<k;h++)
		{
			if(a[g].hour==c[h].hour)
			{
				d[l].hour=a[g].hour;
				d[l].AT1=a[g].AT1;
				d[l].AT2=a[g].AT2;
				d[l].Delta_rh=a[g].Delta_rh;
				d[l].Delta_t=a[g].Delta_t;
				d[l].RH1=a[g].RH1;
				d[l].RH2=a[g].RH2;
				d[l].jd=d[l].hour/24+30/1440;
				d[l].p=c[h].p;
				e=0.01*d[l].RH1*6.112*exp(17.67*d[l].AT1/(243.5+d[l].AT1));
				d[l].SH1=0.622*e/(d[l].p-e*0.36);
				e=0.01*d[l].RH2*6.112*exp(17.67*d[l].AT2/(243.5+d[l].AT2));
				d[l].SH2=0.622*e/(d[l].p-e*0.36);
				l++;
			}
		}
	}



	ofstream outfile("D:\\BIT, Ranchi\\Temperature\\April 2008\\April_2008.txt");

	outfile<<"JD\tAT1\tAT2\tRH1\tRH2\tDelta_T\tDelta_RH\tP\tSH1\tSH2"<<endl;

	for(i=0;i<l;i++)
	{
		outfile<<d[i].jd<<"\t"<<d[i].AT1<<"\t"<<d[i].AT2<<"\t"<<d[i].RH1<<"\t"<<d[i].RH2<<"\t"<<d[i].Delta_t<<"\t"<<d[i].Delta_rh<<"\t"<<d[i].p<<"\t"<<d[i].SH1<<"\t"<<d[i].SH2<<endl;
	}
}

main returns int not void (although that isn't actually causing the error it is quite a serious mistake).

You define infile on both lines 47 and 68. You can only define a variable once in a single code block. You need to either

re-use infile rather than redefining it or use a different variable or (my favourite) split you program up into sensibly sized functions isolating bits of functionality, such as loading 2 files, from each other.

Thanks a lot..
Is it possible to explain the Re-using part...I have used ifstream infile multiple times in a program before, but then it was inside a loop....(for loop)...is it possible that method could work???
can you also tell me how to use a different variable...

it is not about using ifstream multiple times, it is about trying to define the same variable multiple times. ifstream is the type not the variable.

In a for loop it would work because the code body of the loop is executed completely including deleting objects at the end of the iteration for every loop iteration so in a loop you variable infile is created and destroyed on every iteration and there is no attempted redefinition.

To use a different variable just use a different variable name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.