Problem with fscanf()....

Discussion in 'C Programming' started by mohdalibaig@gmail.com, Aug 31, 2007.

  1. Guest

    the program woks fine for a single record but for multiple records it
    isn't reading the entries properly...
    #include<stdio.h>
    #include<conio.h>
    #include<stdlib.h>
    #include<graphics.h>
    void read(void);
    void write(void);
    void main(void)
    {
    clrscr();
    write();
    read();
    getch();
    }
    void write()
    {
    FILE *ptr;
    int id;
    char name[20];
    float sal;
    ptr=fopen("new1.dat","w");
    printf("Press control+z to terminate\nUr Id, Name, Salary:");
    scanf("%d%s%f",&id,name,&sal);
    while(!feof(stdin))
    {
    fprintf(ptr,"%d%s%f",id,name,sal);
    printf("\nId, Name, Salary:");
    scanf("%d%s%f",&id,name,&sal);
    }
    fclose(ptr);

    }
    void read(void)
    {
    FILE *ptr;
    ptr=fopen("new1.dat","r");
    int id2;
    float sal2;
    char name[20];

    while(!feof(ptr))
    {
    fscanf(ptr,"%d%s%f",&id2,name,&sal2);
    printf("%d %s %f\n",id2,name,sal2);

    }
    fclose(ptr);
    }
    , Aug 31, 2007
    #1
    1. Advertising

  2. Richard Bos Guest

    wrote:

    > #include<conio.h>
    > #include<graphics.h>


    > void main(void)
    > clrscr();
    > write();
    > read();
    > getch();
    > }
    > printf("Press control+z to terminate\nUr Id, Name, Salary:");
    > scanf("%d%s%f",&id,name,&sal);
    > while(!feof(stdin))


    You're trolling, right? Please tell me you're trolling...

    Richard
    Richard Bos, Aug 31, 2007
    #2
    1. Advertising

  3. Richard Guest

    (Richard Bos) writes:

    > wrote:
    >
    >> #include<conio.h>
    >> #include<graphics.h>

    >
    >> void main(void)
    >> clrscr();
    >> write();
    >> read();
    >> getch();
    >> }
    >> printf("Press control+z to terminate\nUr Id, Name, Salary:");
    >> scanf("%d%s%f",&id,name,&sal);
    >> while(!feof(stdin))

    >
    > You're trolling, right? Please tell me you're trolling...
    >
    > Richard


    is that a continuation of the other joke, or you're just carping on still?
    Richard, Aug 31, 2007
    #3
  4. <> wrote in message
    news:...
    > the program woks fine for a single record but for multiple records it
    > isn't reading the entries properly...
    > #include<stdio.h>
    > #include<conio.h>
    > #include<stdlib.h>
    > #include<graphics.h>
    > void read(void);
    > void write(void);
    > void main(void)
    > {
    > clrscr();
    > write();
    > read();
    > getch();
    > }
    > void write()
    > {
    > FILE *ptr;
    > int id;
    > char name[20];
    > float sal;
    > ptr=fopen("new1.dat","w");
    > printf("Press control+z to terminate\nUr Id, Name, Salary:");
    > scanf("%d%s%f",&id,name,&sal);
    > while(!feof(stdin))
    > {
    > fprintf(ptr,"%d%s%f",id,name,sal);
    >

    fprintf(ptr, "%d %s %f\n", id, name, sal);
    You might imagine that scanf() is the mirror of printf(), but in fact it is
    not. You have to tell printf() where to put the spaces.

    >
    > printf("\nId, Name, Salary:");
    > scanf("%d%s%f",&id,name,&sal);
    > }
    > fclose(ptr);
    >
    > }
    > void read(void)
    > {
    > FILE *ptr;
    > ptr=fopen("new1.dat","r");
    > int id2;
    > float sal2;
    > char name[20];
    >
    > while(!feof(ptr))
    > {
    > fscanf(ptr,"%d%s%f",&id2,name,&sal2);
    > printf("%d %s %f\n",id2,name,sal2);
    >
    > }
    > fclose(ptr);
    > }
    >


    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Aug 31, 2007
    #4
  5. On 31 Aug, 07:40, wrote:

    assuming you are being serious... Your program has multiple
    problems.

    > the program woks fine for a single record but for multiple records it
    > isn't reading the entries properly...


    - define "fine", "woks" and "isn't reading the entries properly"

    - use of non-standard headers: <conio.h> and <graphics.h>
    - use of non-standard functions clrscr() and getch()
    - bad layout

    then read the comp.lang.c FAQ and fix:

    - void main
    - no return from main
    - failure to check return value of fopen()
    - no \n or fflush() after printf()
    - incorrect use of feof()
    - use of scanf() (it can be used but it is difficult)
    - no check of return value from scanf()
    - use of %s in scanf()
    - use of float rather than double

    and I probably missed something.


    --
    Nick Keighley

    "They consider console off topic, i consider them morons."
    (anon comp.lang.c)
    Nick Keighley, Aug 31, 2007
    #5
  6. Guest

    On Aug 31, 1:10 pm, "Malcolm McLean" <> wrote:
    > <> wrote in message
    >
    > news:...
    >
    >
    >
    > > the program woks fine for a single record but for multiple records it
    > > isn't reading the entries properly...
    > > #include<stdio.h>
    > > #include<conio.h>
    > > #include<stdlib.h>
    > > #include<graphics.h>
    > > void read(void);
    > > void write(void);
    > > void main(void)
    > > {
    > > clrscr();
    > > write();
    > > read();
    > > getch();
    > > }
    > > void write()
    > > {
    > > FILE *ptr;
    > > int id;
    > > char name[20];
    > > float sal;
    > > ptr=fopen("new1.dat","w");
    > > printf("Press control+z to terminate\nUr Id, Name, Salary:");
    > > scanf("%d%s%f",&id,name,&sal);
    > > while(!feof(stdin))
    > > {
    > > fprintf(ptr,"%d%s%f",id,name,sal);

    >
    > fprintf(ptr, "%d %s %f\n", id, name, sal);
    > You might imagine that scanf() is the mirror of printf(), but in fact it is
    > not. You have to tell printf() where to put the spaces.
    >
    >
    >
    >
    >
    >
    >
    > > printf("\nId, Name, Salary:");
    > > scanf("%d%s%f",&id,name,&sal);
    > > }
    > > fclose(ptr);

    >
    > > }
    > > void read(void)
    > > {
    > > FILE *ptr;
    > > ptr=fopen("new1.dat","r");
    > > int id2;
    > > float sal2;
    > > char name[20];

    >
    > > while(!feof(ptr))
    > > {
    > > fscanf(ptr,"%d%s%f",&id2,name,&sal2);
    > > printf("%d %s %f\n",id2,name,sal2);

    >
    > > }
    > > fclose(ptr);
    > > }

    >
    > --
    > Free games and programming goodies.http://www.personal.leeds.ac.uk/~bgy1mm- Hide quoted text -
    >
    > - Show quoted text -- Hide quoted text -
    >
    > - Show quoted text -


    Thanks alot. You have solved my problem. Thanks.
    , Aug 31, 2007
    #6
  7. "Richard Bos" <> wrote in message
    news:4all.nl...
    >> while(!feof(stdin))

    >

    I thought that one was wrong as well. Actually it seems to be right. However
    it probably indicates a poor understanding of feof().

    --
    Free games and programming goodies.
    http://www.personal.leeds.ac.uk/~bgy1mm
    Malcolm McLean, Aug 31, 2007
    #7
  8. CBFalconer Guest

    wrote:
    >
    > the program woks fine for a single record but for multiple records
    > it isn't reading the entries properly...
    > #include<stdio.h>
    > #include<conio.h>


    No such file in standard C

    > #include<stdlib.h>
    > #include<graphics.h>


    No such file in standard C

    > void read(void);
    > void write(void);
    > void main(void)


    main always returns an int.

    Yhese have already made your code incomprehensible. Please remove
    everything that is not standard (and thus OT) before posting it
    again. Graphics are intrinsically system specific, and not dealt
    with here in c.l.c.

    --
    Chuck F (cbfalconer at maineline dot net)
    Available for consulting/temporary embedded and systems.
    <http://cbfalconer.home.att.net>



    --
    Posted via a free Usenet account from http://www.teranews.com
    CBFalconer, Aug 31, 2007
    #8
  9. pete Guest

    wrote:

    > > You might imagine that scanf() is the mirror of printf(),
    > > but in fact it is not.


    The meaning of "%f" is also different for those two functions.
    My two cents.

    --
    pete
    pete, Aug 31, 2007
    #9
  10. On Aug 31, 11:40 am, wrote:
    > the program woks fine for a single record but for multiple records it
    > isn't reading the entries properly...
    > #include<stdio.h>
    > #include<conio.h>
    > #include<stdlib.h>
    > #include<graphics.h>
    > void read(void);
    > void write(void);
    > void main(void)
    > {
    > clrscr();
    > write();
    > read();
    > getch();}
    >
    > void write()
    > {
    > FILE *ptr;
    > int id;
    > char name[20];
    > float sal;
    > ptr=fopen("new1.dat","w");
    > printf("Press control+z to terminate\nUr Id, Name, Salary:");
    > scanf("%d%s%f",&id,name,&sal);
    > while(!feof(stdin))
    > {
    > fprintf(ptr,"%d%s%f",id,name,sal);
    > printf("\nId, Name, Salary:");
    > scanf("%d%s%f",&id,name,&sal);
    > }
    > fclose(ptr);
    >
    > }
    >
    > void read(void)
    > {
    > FILE *ptr;
    > ptr=fopen("new1.dat","r");
    > int id2;
    > float sal2;
    > char name[20];
    >
    > while(!feof(ptr))
    > {
    > fscanf(ptr,"%d%s%f",&id2,name,&sal2);
    > printf("%d %s %f\n",id2,name,sal2);
    >
    > }
    > fclose(ptr);
    >
    > }


    BAD indentation!
    <OT>
    Run ``indent <c-file-name> -ts4 -i4 -bli0'' on your code
    </OT>

    Regards,
    Frodo B
    Frodo Baggins, Aug 31, 2007
    #10
  11. UINDEX Guest

    this program seems writen in TC2.0,which was too old to meet C
    standard.

    > >scanf("%d%s%f",&id,name,&sal);


    it can be more hale writen as scanf("%d,%s,%f",&id,name,&sal);
    add a comma as separator.

    > >while(!feof(stdin))
    > > {
    > > fprintf(ptr,"%d%s%f",id,name,sal);
    > > printf("\nId, Name, Salary:");
    > > scanf("%d%s%f",&id,name,&sal);
    > > }
    > >fclose(ptr);


    and this follows can be:

    if(NULL==ptr){/*out some message to user.*/exit(0);}

    while(!feof(ptr))
    {
    fprintf(ptr,"%d,%s,%f",id,name,sal);
    printf("\nId, Name, Salary:");
    scanf("%d,%s,%f",&id,name,&sal); /*now you need add a comma
    while typing.*/
    }

    fclose(ptr);


    Good Luck

    Kind Regards
    Zeng Jun
    UINDEX, Sep 1, 2007
    #11
  12. UINDEX <> writes:
    > this program seems writen in TC2.0,which was too old to meet C
    > standard.
    >
    >> >scanf("%d%s%f",&id,name,&sal);

    >
    > it can be more hale writen as scanf("%d,%s,%f",&id,name,&sal);
    > add a comma as separator.


    I'm not sure what you mean by "more hale".

    You can make that change if you like, but it changes the meaning of
    the statement. A ',' character in a scanf format string requires a
    literal ',' character in the input. Without the commas, the input
    items may be separated just by whitespace.

    An unqualified "%s" in a scanf format is a buffer overflow waiting to
    happen.

    >> >while(!feof(stdin))
    >> > {
    >> > fprintf(ptr,"%d%s%f",id,name,sal);
    >> > printf("\nId, Name, Salary:");
    >> > scanf("%d%s%f",&id,name,&sal);
    >> > }
    >> >fclose(ptr);

    >
    > and this follows can be:
    >
    > if(NULL==ptr){/*out some message to user.*/exit(0);}
    >
    > while(!feof(ptr))
    > {
    > fprintf(ptr,"%d,%s,%f",id,name,sal);
    > printf("\nId, Name, Salary:");
    > scanf("%d,%s,%f",&id,name,&sal); /*now you need add a comma
    > while typing.*/
    > }
    >
    > fclose(ptr);


    'ptr' appears to be an output file; why are you checking 'feof(ptr)'?
    ('ptr' is also a poor name for a FILE*.)

    In any case, this is a misuse of feof().

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

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    San Diego Supercomputer Center <*> <http://users.sdsc.edu/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Sep 1, 2007
    #12
    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. Thomas Sourmail

    fscanf problem

    Thomas Sourmail, Jun 16, 2004, in forum: C Programming
    Replies:
    7
    Views:
    3,891
    Chris Torek
    Jun 17, 2004
  2. Kay

    fscanf and linked list problem

    Kay, Aug 29, 2004, in forum: C Programming
    Replies:
    7
    Views:
    1,894
  3. learner

    fscanf problem

    learner, Jun 27, 2005, in forum: C Programming
    Replies:
    5
    Views:
    334
    Michael Mair
    Jun 29, 2005
  4. fscanf problem

    , Aug 28, 2005, in forum: C Programming
    Replies:
    2
    Views:
    452
    Walter Roberson
    Aug 28, 2005
  5. rsk

    Problem in using fscanf

    rsk, Jul 30, 2007, in forum: C Programming
    Replies:
    10
    Views:
    621
    Peter 'Shaggy' Haywood
    Aug 3, 2007
Loading...

Share This Page