scanf to include white spaces

J

Jonathan Ng

Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.

Thanks in advance.
 
M

Michael Mair

Hiho,
I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.

Yep. Use a list of permitted/prohibited characters.
Example:
> cat scanblank.c
#include <stdio.h>

int main(void)
{
char string[80];

puts("first test. Enter string: ");
scanf("%[^\t\n]",string);
puts(string);
while(getchar()!=(int)'\n');

puts("\nsecond test. Enter string: ");
scanf("%[ a-zA-Z0-9+*/-]",string);
puts(string);

return 0;
}
>./scanblank
first test. Enter string:
hello, world !
hello, world

second test. Enter string:
abcde j, fghk
abcde j

In the first test, we scan for everything with exception of '\t' and
'\n'. Between d and !, I entered a tabulator. In the second test, we
scan for
a,b,c,..,z,A,B,C,..,Z,0,1,2,..,9,+,*,/,-
You always read at least one of the permitted/non-prohibited characters.
The hyphen inbetween marks a range rather than meaning the hyphen
character. The list must be nonempty.
To scan (or scan not) for ], you have to put it immediately after [ or
[^, respectively.

Remark: I am not checking the return values of puts and scanf.
Especially the latter might be necessary for you.


Cheers
Michael
 
J

Jens.Toerring

Jonathan Ng said:
I was wondering if there was a way to include the white spaces in a string.
Currently, I am using:
scanf("%s", &input);

This looks rather strange. When you have a '%s' format specifier the
variable must be an array pointer. And if 'input' is one than you
would need

scanf( "%s", input );

And I have problems to imagine a situation where 'input' could be
anything else than an array.
However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.

No, scanf() stops at white space. And I would guess that you would be
a lot better of using fgets() here. First of all, when using scanf()
you have no way to check that the string the user enters isn't longer
than there is place in your 'input' array - if that happens scanf()
will happily write beyond the end of the array with impossible to
forsee consequences (including everything to seem to work nicely).
And then you get a whole line of input (or as many characters as fit
into the buffer) and you than can split it up however you need and
aren't handicapped by the limitations of scanf().

Regards, Jens
 
E

Emmanuel Delahaye

Jonathan Ng wrote on 12/08/04 :
Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

This '&' is suspicious. How is 'input' defined?
However, this doesn't include the 'space' character or any other white
spaces. Is there a way I can include the 'space' character rather than skip
in.

For many reasons (including your issue), it's better to get a line from
the user with fgets(). BTW, it's a FAQ.
 
P

pete

Jonathan said:
Hi,

I was wondering if there was a way to include the white spaces in a string.

Currently, I am using:
scanf("%s", &input);

However, this doesn't include the 'space' character or any other white
spaces.
Is there a way I can include the 'space' character rather than skip
in.

/* You can use Pop's Device. */

#include <stdio.h>
#include <stdlib.h>

#define LENGTH 65
#define str(x) # x
#define xstr(x) str(x)

int main(void)
{
int rc;
char array[LENGTH + 1] = "";

fputs("Enter a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
while (rc == 1) {
printf("Your string is: \n%s\n", array);
fputs("Hit Enter key to end, or enter "
"a string with spaces:", stdout);
fflush(stdout);
rc = scanf("%" xstr(LENGTH) "[^\n]%*[^\n]", array);
if (!feof(stdin)) {
getchar();
}
}
return 0;
}
 
D

Dan Pop

In said:
Jonathan Ng wrote on 12/08/04 :

This '&' is suspicious. How is 'input' defined?


For many reasons (including your issue), it's better to get a line from
the user with fgets(). BTW, it's a FAQ.

Pray tell, why is fgets() better than scanf for the OP's issue?

As a matter of fact and excepting gets(), fgets() is *always* the
worst solution, when bullet proof behaviour is desired/need.

Dan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

question about scanf 11
scanf() 2
Scanf Problem 5
scanf internals 11
Why does spacing matter in this context? 0
scanf 2
(f)scanf Question - Grab String of Spaces 12
avoid newline scanf 6

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,039
Latest member
CasimiraVa

Latest Threads

Top