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 ...
- 02-26-2010 #1Just Joined!
- Join Date
- Jan 2010
- Posts
- 11
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....
- 02-26-2010 #2Just 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
- 02-26-2010 #3
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.
- 02-27-2010 #4Just 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.
- 02-27-2010 #5Just 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.
- 02-27-2010 #6Just Joined!
- Join Date
- Feb 2010
- Posts
- 4
We can also achieve it by our own way like the following,
ThanksCode:#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); }
- 02-27-2010 #7Linux Engineer
- 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, drlWelcome - 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 )
- 03-01-2010 #8Just Joined!
- Join Date
- Jan 2010
- Posts
- 11
hi all,
thanks for suggestion...
its realy helpful
- 03-01-2010 #9Just Joined!
- Join Date
- Nov 2008
- Posts
- 29
drl
Sure they thought of it, but it is no part of *C*
- 03-03-2010 #10Just 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


Reply With Quote