473,486 Members | 1,958 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

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

Aug 31 '07 #1
11 2709
mo*********@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
Aug 31 '07 #2
rl*@hoekstra-uitgeverij.nl (Richard Bos) writes:
mo*********@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?
Aug 31 '07 #3

<mo*********@gmail.comwrote in message
news:11*********************@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

Aug 31 '07 #4
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.hand <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)

Aug 31 '07 #5
On Aug 31, 1:10 pm, "Malcolm McLean" <regniz...@btinternet.comwrote:
<mohdalib...@gmail.comwrote in message

news:11*********************@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.

Aug 31 '07 #6

"Richard Bos" <rl*@hoekstra-uitgeverij.nlwrote in message
news:46***************@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

Aug 31 '07 #7
mo*********@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

Aug 31 '07 #8
mo*********@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
Aug 31 '07 #9
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

Aug 31 '07 #10
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

Sep 1 '07 #11
UINDEX <Ui****@gmail.comwrites:
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) ks***@mib.org <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"
Sep 1 '07 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
5385
by: Thomas Sourmail | last post by:
Hi, I hope I am missing something simple, but.. here is my problem: I need my program to check the last column of a file, as in : a b c d target ref 0 0 0 0 1 a 1 0 0 0 1.5 b 2 0 0 0 2 c
2
4073
by: ArmedCoder | last post by:
Hi, im just learning about multithreading for a program i am writin that needs to read from multiple camreas attached to the computer a the same time. I know how to create threads and pass around...
7
2799
by: Kay | last post by:
1) If i want to read data from a txt file, eg John; 23; a Mary; 16; i How can I read the above data stopping reading b4 each semi-colon and save it in three different variables ? 2) If I...
23
2381
by: Babak | last post by:
Hi Everyone, I've written a standard C code for a simple finite element analysis in MSVC++ . When I save the file as a cpp file, it compiles and runs perfectly, but when I save it as a c file,...
14
2902
by: abhishekkarnik | last post by:
Hi, I am trying to read an exe file and print it out character by character in hexadecimal format. The file goes something like this in hexadecimal 0x4d 0x5a 0x90 0x00 0x03 .... so on When I...
18
2064
by: Scott | last post by:
Hi, a problem with this following code is really bugging me. tform = fopen(country, "r"); fseek(tform, 9L, SEEK_SET); fgets(player2, 38, tform); printf("Player Name (save): %s", player);...
10
3637
by: rsk | last post by:
Hi Friends, I have written a code which suppose to read all the numbers from a hex file,But to my surprise the code is skiping every alternate value.Don't know why? Can you please help me in...
4
1415
by: neha_chhatre | last post by:
i have a wierd problem...iam not able to understand wat is goin wrong...i have written a code which reads the values form a text file (it contains lot of values like time=1.147279,value=240.66 and...
49
3497
by: Bert | last post by:
Problem is documented at http://ace.delos.com/usacoprob2?a=deAhyErCKsK&S=gift1 Why doesn't this work? /* ID: albert.4 LANG: C TASK: gift1 */
24
5960
by: kindrain | last post by:
the code checks whether a.txt has exact the same lines, then write different lines into b.txt Here it is: (before that ,you should creat a.txt) ---------------------- #include<stdio.h>...
0
7123
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
6842
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
7319
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
5430
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
1
4864
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4559
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3070
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1378
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
598
muto222
php
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.