Master
Master

Reputation: 2163

fgets() reads fewer characters than expected

I created function that takes in a char pointer. I first get the string value from fgets, the input i'm entering is "rwx". When I enter that value, the strlen of it says it's length is 2 and when I look at the first index of the char array, it returns rw instead of r. May I ask where abouts am I going wrong for iterating through the string?

What I've tried:

int main()
{
    char  access[3];

    while (ValidateAccess(access))
    {
        printf("Please enter your access\n");
        fgets (access, 3, stdin);
    }
    return 0;
}

int ValidateAccess(char * access)
{
    int length = strlen(access);
    int r = 0,w = 0,x = 0,i;

    printf("this is the length %d\n", length);

    if (length == 0)
        return 1;

    for(i = 0; i < length; i++)
    {
        printf("this is the character: %s", &access[i]);

        if (strcmp(&access[i], "r") == 0)
            r++;
        else if (strcmp(&access[i], "w") == 0)
            w++;
        else if (strcmp(&access[i], "x") == 0)
            x++;
    }

    if (r <=1 && w <= 1 && x <= 1 )
        return 0;
    else
        return 1;
}

when the program is ran this is the output

"this is the length 0"
Please enter your access
rwx
this is the length 2
this is the character: rw

Upvotes: 0

Views: 1120

Answers (1)

avysk
avysk

Reputation: 2035

man fgets is a very useful reading. Let me quote: "fgets() reads in at most one less than size characters from stream..."

The strings in C are expected to be terminated with \0 (zero byte). When you define access as an array of 3 elements, it has a room for a string of length 2 -- two characters plus terminating zero byte. When you call fgets saying that you have the space for 3 bytes, it reads two characters, puts them in the first two bytes, and puts terminating zero in the third byte.

Define access a having 4 bytes, and pass 4 to fgets.

Additionally, you are printing string not a char, so it prints everything to the terminating zero byte (that's where rw you see comes from). If you want to print a single character, use %c in format string, not %s (and you should pass a character, not a pointer then).

Try the following program and make sure you understand the output.

#include <stdio.h>

int main() {
        char *foo = "barbaz";

        printf("%c\n", foo[2]);
        printf("%s\n", foo + 2);
}

Upvotes: 2

Related Questions