Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > Problem with fscanf()....

Reply
Thread Tools

Problem with fscanf()....

 
 
mohdalibaig@gmail.com
Guest
Posts: n/a
 
      08-31-2007
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);
}

 
Reply With Quote
 
 
 
 
Richard Bos
Guest
Posts: n/a
 
      08-31-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) 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
 
Reply With Quote
 
 
 
 
Richard
Guest
Posts: n/a
 
      08-31-2007
(E-Mail Removed) (Richard Bos) writes:

> (E-Mail Removed) 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?
 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      08-31-2007

<(E-Mail Removed)> wrote in message
news:(E-Mail Removed) ps.com...
> 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

 
Reply With Quote
 
Nick Keighley
Guest
Posts: n/a
 
      08-31-2007
On 31 Aug, 07:40, (E-Mail Removed) 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)

 
Reply With Quote
 
mohdalibaig@gmail.com
Guest
Posts: n/a
 
      08-31-2007
On Aug 31, 1:10 pm, "Malcolm McLean" <(E-Mail Removed)> wrote:
> <(E-Mail Removed)> wrote in message
>
> news:(E-Mail Removed) ps.com...
>
>
>
> > 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.

 
Reply With Quote
 
Malcolm McLean
Guest
Posts: n/a
 
      08-31-2007

"Richard Bos" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed)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

 
Reply With Quote
 
CBFalconer
Guest
Posts: n/a
 
      08-31-2007
(E-Mail Removed) 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

 
Reply With Quote
 
pete
Guest
Posts: n/a
 
      08-31-2007
(E-Mail Removed) 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
 
Reply With Quote
 
Frodo Baggins
Guest
Posts: n/a
 
      08-31-2007
On Aug 31, 11:40 am, (E-Mail Removed) 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem problem problem :( Need Help Mike ASP General 2 05-11-2004 08:36 AM



Advertisments