I h开发者_StackOverflow社区ave this but once it reaches the supposed EOF it just repeats the loop and scanf again.
int main(void)
{
char words[16];
while(scanf("%15s", words) == 1)
printf("%s\n", words);
return 0;
}
Try:
while(scanf("%15s", words) != EOF)
You need to compare scanf
output with EOF
Since you are specifying a width of 15
in the format string, you'll read at most 15 char. So the words char array should be of size 16
( 15 +1
for null
char). So declare it as:
char words[16];
Scanf is pretty much always more trouble than it's worth. Here are two better ways to do what you're trying to do. This first one is a more-or-less direct translation of your code. It's longer, but you can look at it and see clearly what it does, unlike with scanf.
#include <stdio.h>
#include <ctype.h>
int main(void)
{
char buf[1024], *p, *q;
while (fgets(buf, 1024, stdin))
{
p = buf;
while (*p)
{
while (*p && isspace(*p)) p++;
q = p;
while (*q && !isspace(*q)) q++;
*q = '\0';
if (p != q)
puts(p);
p = q;
}
}
return 0;
}
And here's another version. It's a little harder to see what this does by inspection, but it does not break if a line is longer than 1024 characters, so it's the code I would use in production. (Well, really what I would use in production is tr -s '[:space:]' '\n'
, but this is how you implement something like that.)
#include <stdio.h>
#include <ctype.h>
int main(void)
{
int ch, lastch = '\0';
while ((ch = getchar()) != EOF)
{
if (!isspace(ch))
putchar(ch);
if (!isspace(lastch))
putchar('\n');
lastch = ch;
}
if (lastch != '\0' && !isspace(lastch))
putchar('\n');
return 0;
}
Your code loops until it reads a single word, then exits. So if you give it multiple words it will read the first and exit, while if you give it an empty input, it will loop forever. In any case, it will only print random garbage from uninitialized memory. This is apparently not what you want, but what do you want? If you just want to read and print the first word (if it exists), use if:
if (scanf("%15s", word) == 1)
printf("%s\n", word);
If you want to loop as long as you can read a word, use while:
while (scanf("%15s", word) == 1)
printf("%s\n", word);
Also, as others have noted, you need to give the word array a size that is big enough for your scanf:
char word[16];
Others have suggested testing for EOF instead of checking how many items scanf matched. That's fine for this case, where scanf can't fail to match unless there's an EOF, but is not so good in other cases (such as trying to read integers), where scanf might match nothing without reaching EOF (if the input isn't a number) and return 0.
edit
Looks like you changed your question to match my code which works fine when I run it -- loops reading words until EOF is reached and then exits. So something else is going on with your code, perhaps related to how you are feeding it input as suggested by David
Man, if you are using Windows, EOF
is not reached by pressing enter, but by pressing Crtl+Z at the console. This will print "^Z", an indicator of EOF
. The behavior of functions when reading this (the EOF
or Crtl+Z):
Function | Output |
---|---|
scanf(...) |
EOF |
gets(<variable>) |
NULL |
feof(stdin) |
1 |
getchar() |
EOF |
You need to check the return value against EOF
, not against 1
.
Note that in your example, you also used two different variable names, words
and word
, only declared words
, and didn't declare its length, which should be 16 to fit the 15 characters read in plus a NUL
character.
I guess best way to do this is ...
int main()
{
char str[100];
scanf("[^EOF]",str);
printf("%s",str);
return 0;
}
For C users, this will also work
while ( gets(str) != NULL )
精彩评论