Re: off topic: How to read input from keyboard by the Std. C?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



> On Fri, 10 Dec 2004 16:14:36 -0200, Vinicius <cviniciusm@xxxxxxxxxxxx> wrote:
> > Hello,
> > 
> > how to read input from keyboard (through a console) by the standard
> > language C, please? I suppose an experienced Unix/Linux programmer has
> > the answer.
> > 
On Fri, Dec 10, 2004 at 08:16:43PM +0200, Uri Kogan wrote:
> use fscanf(stdin, ...)

Easiest to use and which is still safe is fgets, example:

/*  THIS example is a working program.  You can paste it into a file and
compile it and it will work as is. */
#include <stdio.h>
main (int argc, char ** argv) {
char mystring[20000];
void * x;

        x = fgets( mystring, 20000, stdin);
        printf("Mystring was %s\n",mystring);
}
/* End of example*/


(note excessively long string buffer mystring, - This is a hangover
from the days when people routinely used "gets". "gets" allowed buffer
overruns and so people routinely allocated extremely huge string buffers
to avoid crashes from the buffer overruns.

fgets avoids the overruns because it will not read in any more than
"n" bytes, where n is the integer you pass to fgets as the second
argument.

"stdin" is a "FILE *" that is always open on standard input.  You don't
need to declare it or anything

see "man fgets"


-- 
Linux/Open Source:  Your infrastructure belongs to you, free, forever.
Idealism:  "Realism applied over a longer time period"
http://www.scaled.com/projects/tierone/
<a href=http://kinz.org>Kinz</a>
http://www.fedoratracker.org http://www.fedorafaq.org
http://www.fedoranews.org
Jeff Kinz, Emergent Research, Hudson, MA.
~
~
~
~


[Index of Archives]     [Current Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Yosemite News]     [Yosemite Photos]     [KDE Users]     [Fedora Tools]     [Fedora Docs]

  Powered by Linux