Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Problem with fscanf().... (http://www.velocityreviews.com/forums/t533914-problem-with-fscanf.html)

mohdalibaig@gmail.com 08-31-2007 06:40 AM

Problem with fscanf()....
 
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);
}


Richard Bos 08-31-2007 07:34 AM

Re: Problem with fscanf()....
 
mohdalibaig@gmail.com 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 08-31-2007 08:06 AM

Re: Problem with fscanf()....
 
rlb@hoekstra-uitgeverij.nl (Richard Bos) writes:

> mohdalibaig@gmail.com 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?

Malcolm McLean 08-31-2007 08:10 AM

Re: Problem with fscanf()....
 

<mohdalibaig@gmail.com> wrote in message
news:1188542418.770634.30260@q3g2000prf.googlegrou 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


Nick Keighley 08-31-2007 08:14 AM

Re: Problem with fscanf()....
 
On 31 Aug, 07:40, mohdalib...@gmail.com 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)


mohdalibaig@gmail.com 08-31-2007 08:43 AM

Re: Problem with fscanf()....
 
On Aug 31, 1:10 pm, "Malcolm McLean" <regniz...@btinternet.com> wrote:
> <mohdalib...@gmail.com> wrote in message
>
> news:1188542418.770634.30260@q3g2000prf.googlegrou 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.


Malcolm McLean 08-31-2007 09:01 AM

Re: Problem with fscanf()....
 

"Richard Bos" <rlb@hoekstra-uitgeverij.nl> wrote in message
news:46d7c438.91089856@news.xs4all.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


CBFalconer 08-31-2007 09:24 AM

Re: Problem with fscanf()....
 
mohdalibaig@gmail.com 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


pete 08-31-2007 10:43 AM

Re: Problem with fscanf()....
 
mohdalibaig@gmail.com 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

Frodo Baggins 08-31-2007 02:57 PM

Re: Problem with fscanf()....
 
On Aug 31, 11:40 am, mohdalib...@gmail.com 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



All times are GMT. The time now is 12:07 PM.

Powered by vBulletin®. Copyright ©2000 - 2013, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.