problem in a code with filehandling

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • neha_chhatre@yahoo.co.in

    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,v alue=240.66 and so
    on............. ...)
    the code is workin well for the firs five iterations...bu t 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.0472 789115646258,t2 =0.0,sum=0.0,av g=0.0,check=0.0 ;

    fp=fopen("pt1.t xt","rb");
    if (fp==NULL)
    perror ("Error opening file: pt1.txt");
    else
    {
    while(!feof(fp) )
    {

    fscanf (fp, "%s", str);
    i=strcmp(str,"t ime");
    if(i==0)
    {
    printf("t1=%f\n ",t1);
    fscanf (fp, "%s", str);
    fscanf (fp, "%f",&t);


    fscanf (fp, "%s", str);
    j=strcmp(str,"v alue");


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

  • Mark McIntyre

    #2
    Re: problem in a code with filehandling

    neha_chhatre@ya hoo.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,v alue=240.66 and so
    on............. ...)
    the code is workin well for the firs five iterations...bu t 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.0472 789115646258,t2 =0.0,sum=0.0,av g=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 >

    Comment

    • Bartc

      #3
      Re: problem in a code with filehandling


      <neha_chhatre@y ahoo.co.inwrote in message
      news:23108ade-0319-4612-b6cf-16ffe2816614@s3 7g2000prg.googl egroups.com...
      On Feb 16, 5:17 pm, Mark McIntyre <markmcint...@s pamcop.netwrote :
      >neha_chha...@y ahoo.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,v alue=240.66 and so
      on............. ...)
      the code is workin well for the firs five iterations...bu t 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


      Comment

      • Ben Bacarisse

        #4
        Re: problem in a code with filehandling

        neha_chhatre@ya hoo.co.in writes:
        the code is workin well for the firs five iterations...bu t 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.0472 789115646258,t2 =0.0,sum=0.0,av g=0.0,check=0.0 ;
        >
        fp=fopen("pt1.t xt","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,"t ime");
        if(i==0)
        {
        printf("t1=%f\n ",t1);
        fscanf (fp, "%s", str);
        fscanf (fp, "%f",&t);
        >
        >
        fscanf (fp, "%s", str);
        j=strcmp(str,"v alue");
        >
        >
        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.

        Comment

        • Herbert Rosenau

          #5
          Re: problem in a code with filehandling

          On Sat, 16 Feb 2008 12:02:39 UTC, neha_chhatre@ya hoo.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,v alue=240.66 and so
          on............. ...)
          the code is workin well for the firs five iterations...bu t 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.0472 789115646258,t2 =0.0,sum=0.0,av g=0.0,check=0.0 ;
          >
          fp=fopen("pt1.t xt","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,"t ime");
          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,"v alue");
          >
          >
          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!

          Comment

          Working...