problem in a code with filehandling

Discussion in 'C Programming' started by neha_chhatre@yahoo.co.in, Feb 16, 2008.

  1. Guest

    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, 2008
    #1
    1. Advertising

  2. 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>
     
    Mark McIntyre, Feb 16, 2008
    #2
    1. Advertising

  3. Guest

    On Feb 16, 5:17 pm, Mark McIntyre <> wrote:
    > 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>




    the following is the file from which it is
    reading...........................

    File type = "ooTextFile"
    Object class = "PitchTier"

    xmin = 0
    xmax = 6.394557823129252
    points: size = 35
    points [1]:
    time = 1.1472789115646258
    value = 240.66658330751105
    points [2]:
    time = 1.2472789115646257
    value = 216.47978246677724
    points [3]:
    time = 1.3472789115646258
    value = 213.83812093716932
    points [4]:
    time = 1.5472789115646257
    value = 210.37648018254478
    points [5]:
    time = 1.6472789115646258
    value = 208.79273834549744
    points [6]:
    time = 1.8472789115646258
    value = 293.3823227689563
    points [7]:
    time = 1.9472789115646258
    value = 316.2633425010987
    points [8]:
    time = 2.2472789115646257
    value = 308.46478338913266
    points [9]:
    time = 2.3472789115646258
    value = 307.8444703977845
    points [10]:
    time = 2.547278911564626
    value = 338.96553268786244
    points [11]:
    time = 2.647278911564626
    value = 337.6384445324801
    points [12]:
    time = 2.7472789115646257
    value = 320.43356977473394
    points [13]:
    time = 2.8472789115646258
    value = 350.33687575048003
    points [14]:
    time = 2.947278911564626
    value = 339.12697914942277
    points [15]:
    time = 3.047278911564626
    value = 342.5515254707035
    points [16]:
    time = 3.2472789115646257
    value = 290.77981793170835
    points [17]:
    time = 3.3472789115646258
    value = 293.72811030038724
    points [18]:
    time = 3.447278911564626
    value = 303.46745549186284
    points [19]:
    time = 3.947278911564626
    value = 251.79416408537938
    points [20]:
    time = 4.047278911564626
    value = 262.28892941664037
    points [21]:
    time = 4.147278911564626
    value = 263.794192151598
    points [22]:
    time = 4.347278911564626
    value = 265.84866240601593
    points [23]:
    time = 4.447278911564626
    value = 266.4690280226695
    points [24]:
    time = 4.647278911564626
    value = 248.27757140917547
    points [25]:
    time = 4.747278911564626
    value = 247.62318109614935
    points [26]:
    time = 4.947278911564626
    value = 251.98088433688451
    points [27]:
    time = 5.047278911564626
    value = 247.5383244834772
    points [28]:
    time = 5.147278911564626
    value = 250.96662855461412
    points [29]:
    time = 5.347278911564626
    value = 223.52314951535087
    points [30]:
    time = 5.447278911564626
    value = 217.52840276529471
    points [31]:
    time = 5.647278911564626
    value = 218.32213379066525
    points [32]:
    time = 5.747278911564626
    value = 219.62413166853094
    points [33]:
    time = 5.947278911564626
    value = 195.49632735594062
    points [34]:
    time = 6.047278911564626
    value = 198.72166439274508
    points [35]:
    time = 6.147278911564626
    value = 202.25686901387476
     
    , Feb 16, 2008
    #3
  4. Bartc Guest

    <> wrote in message
    news:...
    > On Feb 16, 5:17 pm, Mark McIntyre <> wrote:
    >> 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
     
    Bartc, Feb 16, 2008
    #4
  5. wrote:
    > On Feb 16, 5:17 pm, Mark McIntyre <> wrote:
    >> 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.
    >>

    > the following is the file from which it is
    > reading...........................
    >
    > File type = "ooTextFile"
    > Object class = "PitchTier"
    >
    > xmin = 0
    > xmax = 6.394557823129252
    > points: size = 35
    > points [1]:
    > time = 1.1472789115646258
    > value = 240.66658330751105


    You can't store these entire values in a float which has precision
    typically of around 6 sig figs. You can't store them in a double either,
    but the error will be smaller. In both cases, the actual value stored
    will be truncated, and when you do maths with it, the discrepancy will
    scale up accordingly.

    Also you still didn't tell us the error, or why you think its wrong.
     
    Mark McIntyre, Feb 16, 2008
    #5
  6. 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.
     
    Ben Bacarisse, Feb 16, 2008
    #6
  7. On Sat, 16 Feb 2008 12:02:39 UTC, 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!
     
    Herbert Rosenau, Feb 17, 2008
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=

    Fire Code behind code AND Javascript code associated to a Button Click Event

    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=, Feb 10, 2004, in forum: ASP .Net
    Replies:
    4
    Views:
    21,254
    =?Utf-8?B?Q2FybG8gTWFyY2hlc29uaQ==?=
    Feb 11, 2004
  2. Alan Silver
    Replies:
    1
    Views:
    1,708
    Alan Silver
    Sep 15, 2005
  3. keithb
    Replies:
    1
    Views:
    928
    Bruce Barker
    Mar 29, 2006
  4. Replies:
    0
    Views:
    464
  5. TVS

    about filehandling

    TVS, Mar 4, 2005, in forum: C++
    Replies:
    3
    Views:
    459
    Dietmar Kuehl
    Mar 4, 2005
Loading...

Share This Page