LinuxQuestions.org
Help answer threads with 0 replies.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 07-03-2002, 11:12 AM   #1
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Rep: Reputation: 30
Is it safe to use scanf for anything??


I read in a few books before that scanf wasn't such a good choice in input readers. Is there a situation where one would prefer to use scanf?

Up until now since I'm fairly new to C I've been using it for the bulk of input.

thank you
 
Old 07-03-2002, 12:39 PM   #2
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
Rather not. Scanf is good if you're 100% sure that the imput will be exactly what you want. Of course, you never are. Even when you ask an user to enter 'y' or 'n' (char), you can get '7' or "aaaa". So scanf is not a good choce.

BTW It looks it's my 1000th post... There's someone who asked me to be mentioned there. Yes, so he is

Last edited by Mara; 07-03-2002 at 12:43 PM.
 
Old 07-03-2002, 01:06 PM   #3
biosx
Member
 
Registered: Jul 2002
Location: Chicagoland
Distribution: Gentoo, Ubuntu
Posts: 63

Rep: Reputation: 15
Yeah, I choose not to use scanf() in most of my programs. Instead I use the fgets() function and read from stdin like so:

Code:
#include <stdio.h>

int main(void)
{
    char string[80];

    printf("Enter a string: ");
    fgets(string, sizeof(string), stdin);

    printf("\n\nYou entered: %s");
   
    return 0;
}
The only problem with fgets() is that it captures the newline and includes it into your string. However, getting rid of that newline is simple. Good luck!
 
Old 07-03-2002, 02:19 PM   #4
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thanks brothers and sisters!
 
Old 07-03-2002, 03:18 PM   #5
Mara
Moderator
 
Registered: Feb 2002
Location: Grenoble
Distribution: Debian
Posts: 9,696

Rep: Reputation: 232Reputation: 232Reputation: 232
After you have a line (using fgets or your own function) you need ito conver it to the format you'd like to have. It means reading char by char and checking everything correct. It's boring. In my opinion it's the worst part of a program.
 
Old 07-03-2002, 06:19 PM   #6
purpleburple
Member
 
Registered: Jun 2002
Location: USA
Distribution: Slackware8.1
Posts: 332

Original Poster
Rep: Reputation: 30
thnks Mara!
 
  


Reply



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 Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
How to use the scanf( ) int C ? naihe2010 Programming 7 11-30-2005 02:35 AM
problem with scanf fssengg Programming 3 03-29-2005 04:27 AM
scanf question jnusa Programming 4 01-07-2005 09:04 AM
scanf blackzone Programming 1 08-04-2004 01:50 AM
scanf help homerz Programming 2 11-11-2003 05:48 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 04:38 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration