|
From: Per W. <pw...@ia...> - 2004-12-21 08:19:07
|
First of all, check the return value of fscanf() to see if one word was
retrieved - the last time it will return 0 and your code prints the
previous contents of the word buffer.
Second. Don't use fscanf() like that - if someone enters a word longer
than 199 characters you are smoked. Either specify a maximum number of
allowed characters for the word buffer, or rewrite your code to use
fgets() instead.
The world is full of buffer-overflows without the need to produce more
virus doors ;)
/Per W
On Tue, 21 Dec 2004, Eduardo Rebellis wrote:
> Hi everybody,
>
> I'm trying out the following piece of code:
>
> #include <stdio.h>
> #include <stdlib.h>
>
> int main() {
> FILE *fp;
> char word[100];
>
> if((fp = fopen("mystuff.txt", "r")) == NULL) {
> printf("Error while opening file");
> exit(1);
> }
>
> while(!feof(fp)) {
> fscanf(fp, "%s", &word);
> printf(" %s ", word);
> }
>
> return 0;
> }
|