File handling code

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

  1. Guest

    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.txt","rb");

    while(!feof(fp))
    {

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


    }


    }

    please send reply as soon as possible its urgent
     
    , Feb 14, 2008
    #1
    1. Advertising

  2. In article <>,
    <> wrote:
    >hey please help me execute the code on ubuntu in C


    >#include<stdio.h>
    >#include<stdlib.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.txt","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",ticktock);
    > 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


    http://www.officeplayground.com/lackplanning.html
    --
    "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
     
    Walter Roberson, Feb 14, 2008
    #2
    1. Advertising

  3. santosh Guest

    wrote:

    > hey please help me execute the code on ubuntu in C
    >
    > #include<stdio.h>
    > #include<stdlib.h>


    Why don't you use space for readability?

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


    Do check all library calls. A file open fail for many reasons.

    > while(!feof(fp))


    This is wrong. In C you use feof (and ferror) *after* an I/O function
    call has returned whatever value it uses to signal failure.

    For example getc returns EOF on either end-of-file or an unspecified
    error. You can call feof and ferror *after* a getc call has returned
    EOF to find out which condition is true.

    > {
    >
    > sscanf(fp, "time=%f value=%d", &ticktock, &val);


    sscanf reads from a string in memory. You probably want fscanf.
    Also check all these calls for failure. Failing to do so and proceeding,
    assuming success, can cause hard to trace problems.

    > printf("%f",ticktock);
    > printf("%d",val);


    Some spacing between these outputs would help readability.

    >
    >
    > }


    You should have a return statement here.

    Use the -W, -Wall, -ansi and -pedantic flags with gcc to get it to
    report potential problems that it might otherwise keep silent about.

    >
    > }
    >
    > please send reply as soon as possible its urgent


    See:

    <http://www.c-faq.com/>
     
    santosh, Feb 14, 2008
    #3
  4. writes:
    [...]
    > while(!feof(fp))

    [...]

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

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Feb 14, 2008
    #4
  5. Bartc Guest

    <> wrote in message
    news:...
    > 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.txt","rb");
    >
    > while(!feof(fp))
    > {
    >
    > sscanf(fp, "time=%f value=%d", &ticktock, &val);
    > printf("%f",ticktock);
    > 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.txt","rb");

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

    --
    Bart
     
    Bartc, Feb 14, 2008
    #5
    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. keithb
    Replies:
    1
    Views:
    940
    Bruce Barker
    Mar 29, 2006
  2. Replies:
    1
    Views:
    431
    Alan Balmer
    Apr 19, 2005
  3. Mark Tarver
    Replies:
    22
    Views:
    1,336
    J Kenneth King
    Apr 26, 2009
  4. Peter
    Replies:
    34
    Views:
    1,974
    James Kanze
    Oct 17, 2009
  5. Iñaki Baz Castillo
    Replies:
    1
    Views:
    200
    Iñaki Baz Castillo
    Apr 15, 2008
Loading...

Share This Page