One of my assignments for writing my own UNIX shell. To get input from a user, I use fgets to capture input as a string, but I'm not quite sure how this works. When I run:
char command[50]; fgets(command, sizeof(command), stdin); printf("Your Command: %s", &command); int length = strlen(command); printf("Length of String: %d\n", length);
Let's say my entry was "exit". strlen says the string is 5 characters long, not four. I want to do this:
if( (strcmp(command, "exit")) == 0 ){ doSomething(); }
but the command will never be equal to the line in which I want; as if he has an unknown character whom I am not sure. Is it a null character at the end? How to change the if statement to verify that the user input captured with fgets is "exit"? Thanks!
source share