File handling code

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

    File handling code

    hey please help me execute the code on ubuntu in C

    #include<stdio. h>
    #include<stdlib .h>

    int main()
    {
    FILE *fp;
    char ch;
    float ticktock;
    int val;
    fp=fopen("pt1.t xt","rb");

    while(!feof(fp) )
    {

    sscanf(fp, "time=%f value=%d", &ticktock, &val);
    printf("%f",tic ktock);
    printf("%d",val );


    }


    }

    please send reply as soon as possible its urgent
  • Walter Roberson

    #2
    Re: File handling code

    In article <7a11005a-0f1c-4dc7-a54b-5a2bd70f77c3@e6 g2000prf.google groups.com>,
    <neha_chhatre@y ahoo.co.inwrote :
    >hey please help me execute the code on ubuntu in C
    >#include<stdio .h>
    >#include<stdli b.h>
    >
    >int main()
    Usually you would use int main(void)
    >{
    > FILE *fp;
    > char ch;
    You do not appear to use 'ch', so you might as well get rid of it.
    > float ticktock;
    > int val;
    > fp=fopen("pt1.t xt","rb");
    You fail to check to see whether the open succeeded by checking
    the value of fp after the fopen().
    > while(!feof(fp) )
    feof() in C never makes a -prediction- about whether the next read
    would work or not (the fact that no data is available in the buffer
    right now does not mean that end of file has been reached -- you
    might be connected to a network socket or the action of asking for
    more data might cause more data to be released to you.) Therefore
    testing for feof() first before the read does not work:
    instead you need to check the return value of each file read
    to see whether you got the data you wanted.
    > {
    >
    > sscanf(fp, "time=%f value=%d", &ticktock, &val);
    > printf("%f",tic ktock);
    > printf("%d",val );
    Typically if you have two successive printf()'s, you would merge
    them into a single call, such as
    printf("%f%d, ticktock, val);
    > }
    Oh dear, you did not include any spacing between the output
    values and you do not include and line termination characters.
    Everything is going to be output as a single long string of
    digits (with some decimal points in places.)


    You declared main as returning an int but you failed to return
    any value. That gives undefined behaviour for C90 (but will work
    in C99.)
    >}
    >please send reply as soon as possible its urgent

    --
    "The human mind is so strangely capricious, that, when freed from
    the pressure of real misery, it becomes open and sensitive to the
    ideal apprehension of ideal calamities." -- Sir Walter Scott

    Comment

    • Keith Thompson

      #3
      Re: File handling code

      neha_chhatre@ya hoo.co.in writes:
      [...]
      while(!feof(fp) )
      [...]

      Please read section 12 of the comp.lang.c FAQ, <http://www.c-faq.com/>.

      --
      Keith Thompson (The_Other_Keit h) <kst-u@mib.org>
      Nokia
      "We must do something. This is something. Therefore, we must do this."
      -- Antony Jay and Jonathan Lynn, "Yes Minister"

      Comment

      • Bartc

        #4
        Re: File handling code


        <neha_chhatre@y ahoo.co.inwrote in message
        news:7a11005a-0f1c-4dc7-a54b-5a2bd70f77c3@e6 g2000prf.google groups.com...
        hey please help me execute the code on ubuntu in C
        >
        #include<stdio. h>
        #include<stdlib .h>
        >
        int main()
        {
        FILE *fp;
        char ch;
        float ticktock;
        int val;
        fp=fopen("pt1.t xt","rb");
        >
        while(!feof(fp) )
        {
        >
        sscanf(fp, "time=%f value=%d", &ticktock, &val);
        printf("%f",tic ktock);
        printf("%d",val );
        }
        }
        You want to read in a text file and print a summary of the contents to the
        screen?

        What is the exact format of the input file, something like this:

        time=1234.56 value=7654
        time=8329.01 value=9127
        ....
        until end of file, with each pair on a new line?

        And you want to print to the screen the following:

        1234.56 7654
        8329.01 9127
        ....?

        (This sounds odd, it's makes more sense for the bare numbers to be in the
        file and the labels to be added to screen output.)

        What do you want to do about blank lines in the input, and incorrect
        formats? It's a good idea to read the actual "time" and "value" labels and
        check these are as expected, as sometimes things can get out of step.

        Sorry can't offer code ideas (file handling is a pig in C with the
        counter-intuitive feof() and inability to properly read a complete line of
        input using fgets()). But it's sometimes useful to fully spell out your
        task.

        But, if all you really want to do is to copy the file unchanged to the
        screen, then this is fairly easy (you don't even need a program really):

        int c;

        fp=fopen("pt1.t xt","rb");

        if (fp!=0)
        {while (1)
        {c=fgetc(fp);
        if (c==EOF) break;
        printf("%c",c);
        };
        fclose(fp);
        }

        --
        Bart



        Comment

        Working...