N
neha_chhatre
i have a wierd problem...iam not able to understand wat is goin
wrong...i have written a code which reads the values form a text file
(it contains lot of values like time=1.147279,value=240.66 and so
on................)
the code is workin well for the firs five iterations...but thn it isnt
doing the computations properly after the 5th iteration (sumthing is
goin wrong in the if loop)
pls help me out with this
Iam posting the entire code here.......
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
char ch,str[100],str1[100];
int c=0,i,j;
float t=0.0,t1=1.0472789115646258,t2=0.0,sum=0.0,avg=0.0,check=0.0;
fp=fopen("pt1.txt","rb");
if (fp==NULL)
perror ("Error opening file: pt1.txt");
else
{
while(!feof(fp))
{
fscanf (fp, "%s", str);
i=strcmp(str,"time");
if(i==0)
{
printf("t1=%f\n",t1);
fscanf (fp, "%s", str);
fscanf (fp, "%f",&t);
fscanf (fp, "%s", str);
j=strcmp(str,"value");
if(j==0)
{
fscanf (fp, "%s", str);
fscanf (fp, "%f",&t2);
printf("t2=%f\n",t2);
}
check=t1+0.1;
printf("t=%f\n",t);
printf("check=%f\n",check);
if(t==check)
{
sum=sum+t2;
c++;
printf("sum=%f\n",sum);
printf("c=%d\n",c);
}
else
{
printf("sum=%f\n",sum);
sum=0.0;
printf("c=%d\n",c);
c=0;
sum=sum+t2;
c++;
}
t1=t;
}
}
fclose(fp);
}
//printf("%d",c);
}
wrong...i have written a code which reads the values form a text file
(it contains lot of values like time=1.147279,value=240.66 and so
on................)
the code is workin well for the firs five iterations...but thn it isnt
doing the computations properly after the 5th iteration (sumthing is
goin wrong in the if loop)
pls help me out with this
Iam posting the entire code here.......
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int main()
{
FILE *fp;
char ch,str[100],str1[100];
int c=0,i,j;
float t=0.0,t1=1.0472789115646258,t2=0.0,sum=0.0,avg=0.0,check=0.0;
fp=fopen("pt1.txt","rb");
if (fp==NULL)
perror ("Error opening file: pt1.txt");
else
{
while(!feof(fp))
{
fscanf (fp, "%s", str);
i=strcmp(str,"time");
if(i==0)
{
printf("t1=%f\n",t1);
fscanf (fp, "%s", str);
fscanf (fp, "%f",&t);
fscanf (fp, "%s", str);
j=strcmp(str,"value");
if(j==0)
{
fscanf (fp, "%s", str);
fscanf (fp, "%f",&t2);
printf("t2=%f\n",t2);
}
check=t1+0.1;
printf("t=%f\n",t);
printf("check=%f\n",check);
if(t==check)
{
sum=sum+t2;
c++;
printf("sum=%f\n",sum);
printf("c=%d\n",c);
}
else
{
printf("sum=%f\n",sum);
sum=0.0;
printf("c=%d\n",c);
c=0;
sum=sum+t2;
c++;
}
t1=t;
}
}
fclose(fp);
}
//printf("%d",c);
}