Problem with fscanf()....

M

mohdalibaig

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);
}
 
R

Richard Bos

#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
 
M

Malcolm McLean

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);
}
 
N

Nick Keighley

On 31 Aug, 07:40, (e-mail address 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.
 
M

mohdalibaig

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.
 
M

Malcolm McLean

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

CBFalconer

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.
 
F

Frodo Baggins

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
 
U

UINDEX

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

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

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
 
K

Keith Thompson

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


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.
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/>.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,997
Messages
2,570,241
Members
46,831
Latest member
RusselWill

Latest Threads

Top