473,387 Members | 1,569 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

problem in a code with filehandling

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);
}

Feb 16 '08 #1
4 1408
ne**********@yahoo.co.in wrote:
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.......
You didn't tell us what the file contained, or what the actual error
was. Post an example of the input and output, and where you think its wrong.
#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;
Float is often a bad choice for mathematical work. The precision is
quite poor, and large errors can rapidly creep in.

--
Mark McIntyre

CLC FAQ <http://c-faq.com/>
CLC readme: <http://www.ungerhu.com/jxh/clc.welcome.txt>
Feb 16 '08 #2

<ne**********@yahoo.co.inwrote in message
news:23**********************************@s37g2000 prg.googlegroups.com...
On Feb 16, 5:17 pm, Mark McIntyre <markmcint...@spamcop.netwrote:
>neha_chha...@yahoo.co.in wrote:
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.......
What exactly is it doing wrong? What should the output be at the sixth
iteration (at points[6] in the file?).
--
Bart
Feb 16 '08 #3
ne**********@yahoo.co.in writes:
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)
On my system it works with the data you posted elsewhere (as far as I
can tell). What is it that goes wrong?
#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))
Not usual the wight way to do input in C. See the FAQ at
http://c-faq.com/ (specifically Q 12.2).
{

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;
This is very convoluted. fscanf can tell you if the input looks right
(i.e. has the right fixed strings in it) and can tell you that the
numbers were read and converted correctly. For example:

do {
switch (fscanf(fp, "time = %f value = %f", &t, &v)) {
case 2:
printf("%f %f\n", t, v); /* change this to do what you want */
break;
case 1:
printf("Time not followed by value.\n");
break;
default:
fgetc(fp);
}
} while (!feof(fp));

It relies on allowing fscanf to fail a lot, but unless you need
blistering speed, that won't be a problem.

--
Ben.
Feb 16 '08 #4
On Sat, 16 Feb 2008 12:02:39 UTC, ne**********@yahoo.co.in wrote:
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))
As you have'nt tried to read a single byte from the file the while
will loop on
{

fscanf (fp, "%s", str);
As you does NOT test the result of fscanf() you continues into
unspecified behavior when it ends with either EOF oer error, so str
may not changed as you expected
i=strcmp(str,"time");
if(i==0)
{
printf("t1=%f\n",t1);
fscanf (fp, "%s", str);
fscanf (fp, "%f",&t);
fscanf (fp, "%s", str);
Same as above; no test for EOF or error is done, so any error or EOF
left undedected.
j=strcmp(str,"value");
if(j==0)
{
fscanf (fp, "%s", str);
fscanf (fp, "%f",&t2);
same as above
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);
}
Nobody can tell what wents wrong because you have too many (excactly
more than 0) attempts to read without a test for success.

--
Tschau/Bye
Herbert

Visit http://www.ecomstation.de the home of german eComStation
eComStation 1.2R Deutsch ist da!
Feb 17 '08 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

11
by: Kostatus | last post by:
I have a virtual function in a base class, which is then overwritten by a function of the same name in a publically derived class. When I call the function using a pointer to the derived class...
7
by: Keith Dewell | last post by:
Greetings! My current job has brought me back to working in C++ which I haven't used since school days. The solution to my problem may be trivial but I have struggled with it for the last two...
6
by: harry | last post by:
Hi, I have a program that runs on multiple client pc's. Occasionally one or more of those pc's use VPN to connect to another corporate network. When using VPN they need to set proxy server in...
28
by: Jon Davis | last post by:
If I have a class with a virtual method, and a child class that overrides the virtual method, and then I create an instance of the child class AS A base class... BaseClass bc = new ChildClass();...
9
by: Rajat Tandon | last post by:
Hello there, I am relatively new to the newsgroups and C#. I have never been disappointed with the groups and always got the prompt replies to my queries.This is yet another strange issue, I am...
5
by: | last post by:
Hi, I'm trying to use the cookie munging session handling behaviour of asp.net instead of cookies themselves as I'm finding quite a few people are barring cookies (especially AOL users). If I...
6
by: Ammar | last post by:
Dear All, I'm facing a small problem. I have a portal web site, that contains articles, for each article, the end user can send a comment about the article. The problem is: I the comment length...
2
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
6
by: TPJ | last post by:
Help me please, because I really don't get it. I think it's some stupid mistake I make, but I just can't find it. I have been thinking about it for three days so far and I still haven't found any...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.