Velocity Reviews - Computer Hardware Reviews

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

Reply
Thread Tools

Problem with fscanf()....

 
 
UINDEX
Guest
Posts: n/a
 
      09-01-2007
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

 
Reply With Quote
 
 
 
 
Keith Thompson
Guest
Posts: n/a
 
      09-01-2007
UINDEX <(E-Mail Removed)> 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.velocityreviews.com/forums/(E-Mail Removed) <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"
 
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