Re: Getting user input in Linux?
- From: Nick Keighley <nick_keighley_nospam@xxxxxxxxxxx>
- Date: Thu, 31 Jan 2008 00:50:41 -0800 (PST)
On 31 Jan, 03:17, Zach <net...@xxxxxxxxx> wrote:
I run Linux 2.6.18 and am seeking a way in C to get user input. I want
them to have the chance to enter a value and have it assigned to a
variable.
I currently have:
int* s1;
printf("Enter first integer: %d\n",s1);
scanf(&s1);
When I run this it automatically enters 0. It never pauses and thus
does not allow me to type in a value.
note scanf() can be tricky. It might be better to use fgets()
followed by atoi() or (better) strtol(). Read the documentation for
them.
Oh, and try and get a copy of K&R
--
Nick Keighley
The fscanf equivalent of fgets is so simple
that it can be used inline whenever needed:-
char s[NN + 1] = "", c;
int rc = fscanf(fp, "%NN[^\n]%1[\n]", s, &c);
if (rc == 1) fscanf("%*[^\n]%*c);
if (rc == 0) getc(fp);
Dan Pop
.
- References:
- Getting user input in Linux?
- From: Zach
- Getting user input in Linux?
- Prev by Date: Re: A solution for the allocation failures problem
- Next by Date: Re: declaration of variable in for loop
- Previous by thread: Re: Getting user input in Linux?
- Next by thread: Re: Getting user input in Linux?
- Index(es):
Relevant Pages
|
|