Reflections of my thoughts: http://www.codereflect.com/2006/10/scanf-in-c-reading-single-line-of-text.html
adamo
· 3 years ago
You have to make sure that: name[99] = '';
Leo Soto
· 2 years ago
It's vulnerable to buffer overflows :(
Jeffrey
· 1 year ago
Use fflush(stdin); to avoid looping!?
Eric
· 1 year ago
THANK YOU. Ive been searching forever online for this!!!!!!!!!!!! you rock
Aia
· 1 year ago
>It’s vulnerable to buffer overflows To avoid that declare a maximum field if ( scanf("%99[^\n]", name) == 1) { /* do something */} will do it. However does leave still input in the stdin buffer if more than 99 keys where typed.
>Use fflush(stdin); to avoid looping!? That's invoking undefined behavior. fflush() is only for OUTPUT streams, and not for INPUT like stdin.
sd
· 1 year ago
Hey thanks a lot for that piece of code! Works great! The strange thing is that the string variable doesn't need a preceeding ampersand (&). Quite surprising....I'd like to know how that happens...
getline to read input from a u
· 1 year ago
[...] (what's surprising is that abc doesn't need an ampersand preceeding it) Got it from here and there are comments about scanf being vulnerable to buffer overflows. Is that true? So [...]
Luis Tellez
· 1 year ago
fflush(stdin) works fine for the input, i have use it in many proyects i haver worked and havent given me any problems.
ashih
· 10 months ago
thnx dear....
jessica
· 10 months ago
you are awsom
Dan D.
· 10 months ago
I wish I'd found this blog posting two hours ago.
Shailesh
· 8 months ago
Awesome. This works great
Will
· 8 months ago
Use fgets instead...
The version that is not vulnerable to buffer overflows is:
char name[100];
printf(”Enter the name:”); fgets(name,100,stdin);
Hope that helps...
Anonymous
· 7 months ago
[...] Datei lesen und an bestimmer Stelle verändern Am besten ist vielleicht, immer ein komplette Zeile einzulesen. Dann musst du immer prüfen, ob du nun an dem zu editierenden Block angekommen bist. Das kann man [...]
diapir
· 6 months ago
Cool reminder. I used this thing to skip comments in a file :
// eat spaces // match '#' // capture and discard everything until end of line fscanf(stream, " #%*[^\n]");
Cheers
sub
· 5 months ago
i didn't get tis.... none of the codes worked for me,including the one containing regular expressions. how do we read a line from the user in C.
ullas
· 5 months ago
Hey this is not working.....
Bubba
· 4 months ago
(To Sub)Two questions back:
Here is a simple snippet to answer your question:
#include
int main(int argc, char *argv[]) { char first_name[100];
printf("What is your name? "); gets(first_name); printf("\nNice to meet you %s!",first_name); return 0; }
Try this, I hope it helps in some small way.....
Bubba
· 4 months ago
Oddly, The post deleted what was in the brackets after include (which was simply "stdio.h")
Bubba
· 4 months ago
Or, we can just use
#include int main() { char name[99]; printf("What is your name? "); scanf("%s",&name); printf("Hello, %s. How are you?\n",name); return(0); }
Same result.
Stepnowski
· 3 months ago
I would like to see more blog entries like this one
name[99] = '';
To avoid that declare a maximum field
if ( scanf("%99[^\n]", name) == 1) { /* do something */} will do it. However does leave still input in the stdin buffer if more than 99 keys where typed.
>Use fflush(stdin); to avoid looping!?
That's invoking undefined behavior.
fflush() is only for OUTPUT streams, and not for INPUT like stdin.
The strange thing is that the string variable doesn't need a preceeding ampersand (&).
Quite surprising....I'd like to know how that happens...
The version that is not vulnerable to buffer overflows is:
char name[100];
printf(”Enter the name:”);
fgets(name,100,stdin);
Hope that helps...
// eat spaces
// match '#'
// capture and discard everything until end of line
fscanf(stream, " #%*[^\n]");
Cheers
how do we read a line from the user in C.
Here is a simple snippet to answer your question:
#include
int main(int argc, char *argv[])
{
char first_name[100];
printf("What is your name? ");
gets(first_name);
printf("\nNice to meet you %s!",first_name);
return 0;
}
Try this, I hope it helps in some small way.....
The post deleted what was in the brackets after include (which was simply "stdio.h")
#include
int main()
{
char name[99];
printf("What is your name? ");
scanf("%s",&name);
printf("Hello, %s. How are you?\n",name);
return(0);
}
Same result.