scanf in C : Reading single line of text using "scanf" function

 

I have done it 4-5 years ago, that to read a whole line of text (including space) using scanf function.

When I was preparing for my exams, I went back to the old days of a “C” student. Nowadays I’m a bit used with C++. So I forgot the format specifier to specfy inside the scanf function. Finally my memory retrieved it back

Here’s it is:

char name[100];

printf(“Enter the name:”);
scanf(“%[^\n]“,name);

Something cool no?

 
  • http://adamo.wordpress.com/ adamo

    You have to make sure that:
    name[99] = ”;

  • http://blog.leosoto.com Leo Soto

    It’s vulnerable to buffer overflows :(

  • Jeffrey

    Use fflush(stdin); to avoid looping!?

  • http://SinglesPlease.com Eric

    THANK YOU. Ive been searching forever online for this!!!!!!!!!!!! you rock

  • Aia

    >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

    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…

  • http://www.unix.com/unix-dummies-questions-answers/82194-getline-read-input-user.html#post302239043 getline to read input from a user – UNIX for Dummies Questions & Answers – The UNIX and Linux Forums

    [...] (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

    fflush(stdin) works fine for the input, i have use it in many proyects i haver worked and havent given me any problems.

  • ashih

    thnx dear….

  • jessica

    you are awsom

  • Dan D.

    I wish I’d found this blog posting two hours ago.

  • Shailesh

    Awesome. This works great

  • Will

    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…

  • http://www.tutorials.de/forum/c-c/340661-datei-lesen-und-bestimmer-stelle-veraendern.html#post1761432 Anonymous

    [...] 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

    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

    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

    Hey this is not working…..

  • Bubba

    (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

    Oddly,
    The post deleted what was in the brackets after include (which was simply “stdio.h”)

  • Bubba

    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.

  • http://PrimeGoldBuyers.com Stepnowski

    I would like to see more blog entries like this one

  • http://twitter.com/paulostradioti Paulo R Stradioti

    Hello,

    Thanks for sharing such a smart and easy way to do a simple task!

    Paulo

  • http://www.air-force-one.com air force 1 shoes

    “Here air jordan 21 products xx, has fashion model, superior quality and service, cheap price and updates quickly.I support strongly always! I want to buy XX, I hesitate to select which style more better.Hope your unique recommends.

  • Themanish43

    printf(“Please Enter Your Name:n”);
    char sname[200];
    scanf(“%s[^n]“,sname);

    ll it work?

  • Mike

    #include
    #include
    int main()
    {
    char text[100]; /* Buffer for a line of input. */
    char filename[14];
    FILE *f;
    printf(“Enter a file name please:n”);
    scanf(“%s”,&filename);
    while ( fgets( text, sizeof(text), stdin ) == “[^n]” );
    printf(“Enter your text: “);
    while ( fgets( text, sizeof(text), stdin ) == “[^n]” );
    f=fopen(filename,”w”);
    fprintf(f,”%s”,text);
    fclose(f);
    system(“Pause”);
    }

    This should work 100% ;)

  • Mike

    #include
    #include
    int main()
    {
    char text[100]; /* Buffer for a line of input. */
    char filename[14];
    FILE *f;
    printf(“Enter a file name please:n”);
    scanf(“%s”,&filename);
    while ( fgets( text, sizeof(text), stdin ) == “[^n]” );
    printf(“Enter your text: “);
    while ( fgets( text, sizeof(text), stdin ) == “[^n]” );
    f=fopen(filename,”w”);
    fprintf(f,”%s”,text);
    fclose(f);
    system(“Pause”);
    }

    This should work 100% :)

  • http://ivan.vucica.net/ Ivan Vucica

    You can’t compare C strings like that. Maybe you meant:

    while (!strcmp(fgets(text, sizeof(text), stdin), “[^n]“);

    althought I have no idea what you meant by that.

  • http://ivan.vucica.net/ Ivan Vucica

    No, you can’t. First of all, scanf() with format “%s” goes until first whitespace. It cannot read in “a string with spaces”; that would produce just “a”.

    Second, you have a beginner’s mistake in there:

    scanf(“%s”,&name);

    “name” is already a pointer, why are you the address of the pointer?

  • http://ivan.vucica.net/ Ivan Vucica

    Thanks! This is one thing I didn’t know.