Find the answer to your Linux question:
Page 1 of 2 1 2 LastLast
Results 1 to 10 of 11
hi all can anyone tell me in c how to read multiple lines from user input using scanf it is not possible and using gets we can able to read ...
  1. #1
    Just Joined!
    Join Date
    Jan 2010
    Posts
    11

    Post read multiple lines from user input in c

    hi all
    can anyone tell me
    in c how to read multiple lines from user input

    using scanf it is not possible and using gets we can able to read one line at a time
    hoe to read multiple lines at a time from user input....

  2. #2
    Just Joined!
    Join Date
    Nov 2008
    Posts
    29
    Arch,

    I/O is buffered by default. So you have to switch it to unbuffered I/O, and then using getc, you can get any key (that generates a character) when the user presses it.

    Have a look at the GNU readline package, it does all sorts of nifty stuff with User I/O.

    Regards,
    Guus

  3. #3
    Trusted Penguin elija's Avatar
    Join Date
    Jul 2004
    Location
    Either at home or at work or down the pub
    Posts
    2,298
    Is there a reason you can't use a loop?
    If we hit that bullseye, the rest of the dominoes will fall like a house of cards. Checkmate! (Zapp Brannigan)


    My new blog. It's probably not as good as I think it is.

  4. #4
    Just Joined!
    Join Date
    Dec 2006
    Posts
    4

    Multiple lines in c

    bytes_read = getline (&buffer, &no_bytes, stdin);

    When there is a stream error getline() returns a -1;

    See The C Programming Language, second edition, by Brian Kernighan and Dennis Ritchie, the C bible by the creators of the language. It's also one of the most readable programming manual yet written. It's only 272 pages, compare that to most C++ books.

  5. #5
    Just Joined!
    Join Date
    Nov 2008
    Posts
    29
    Please note that getline is pure GNU extension. You will not find it in anything written by K&R.

  6. #6
    Just Joined!
    Join Date
    Feb 2010
    Posts
    4

    Thumbs up

    We can also achieve it by our own way like the following,

    Code:
    #include <stdio.h>
    #include<malloc.h>
    main(){
            int c;
            char *line;
            char *ptr;
            line=(char *)malloc(1);
            ptr=line;
            for (;( *line=c = getchar())!= EOF ;line++);
            *line='\0';
            printf("%s",ptr);
    }
    Thanks

  7. #7
    drl
    drl is offline
    Linux Engineer drl's Avatar
    Join Date
    Apr 2006
    Location
    Saint Paul, MN, USA / CentOS, Debian, Solaris, SuSE
    Posts
    1,117

    K & R + getline

    Hi.

    The K & R ( The C programming Language ) does mention function getline in two instances, pages 26 and 67 ff. They are showing how to design such a function. My K & R is from 1978.

    The c function that is supplied on GNU/Linux systems is not quite the same as the one in K & R, and is a GNU extension, see man getline for details ... cheers, drl
    Welcome - get the most out of the forum by reading forum basics and guidelines: click here.
    90% of questions can be answered by using man pages, Quick Search, Advanced Search, Google search, Wikipedia.
    We look forward to helping you with the challenge of the other 10%.
    ( Mn, 2.6.n, AMD-64 3000+, ASUS A8V Deluxe, 1 GB, SATA + IDE, Matrox G400 AGP )

  8. #8
    Just Joined!
    Join Date
    Jan 2010
    Posts
    11
    hi all,
    thanks for suggestion...
    its realy helpful

  9. #9
    Just Joined!
    Join Date
    Nov 2008
    Posts
    29
    drl
    Sure they thought of it, but it is no part of *C*

  10. #10
    Just Joined!
    Join Date
    Dec 2006
    Posts
    4
    My apologies for the confusion. It is included in the IEEE's
    The Open Group Base Specifications Issue 7
    IEEE Std 1003.1-2008
    Copyright © 2001-2008 The IEEE and The Open Group
    This is ANSI standard lib. for C & C++, find it in all ANSI standard stdio.h

Page 1 of 2 1 2 LastLast

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
...