Using fgets Function in C

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!

+4
source share
7 answers

fgets treats the line terminator as a valid character. This is an extra character that you get.

Just do something like command[strlen(command) - 1] = '\0'; to remove the line terminator. Then you can do all your strcmp .

+7
source

On the fgets page:

fgets () reads no more than one character of size from the stream and saves them to the buffer pointed to by s. Reading stops after EOF or a new line. If a new line is read, it is saved in the buffer. A '\ 0' is saved after the last character in the buffer.

Bottom line: you have an extra line at the end of the line when comparing.

+4
source

fgets will always include the line ending character in the input line. You can remove any space, including newlines, from the end of your "command" by doing:

 char command[50]; fgets(command, sizeof(command), stdin); size_t length = strlen(command); // Trim off trailing "spaces" including newline characters while ((length > 0) && isspace(command[length-1])) command[--length] = '\0'; printf("Your Command: %s\n", &command); // Include newline now... // This is computed above... // int length = strlen(command); // Continue as before 
+3
source

fgets also captures line breaks.

Note that you can overcome this in several ways; you can use strncmp :

 if((strncmp(command, "exit", 4)) == 0) 

which checks if only the first 4 characters of the command match (although this may not be the right option for you).

Another tactic is to check with a line break:

 if((strcmp(command, "exit\n")) == 0) 
+2
source

Probably the easiest way to handle this is to switch to using scanf to read input:

 char command[51]; scanf("%50[^\n]", command); if (0 == strcmp(command, "exit")) do_something(); 
+2
source

Your line still has a new line at the end. You can compare with "exit\n" or use something like strncmp(command, "exit", 4) . Note that this will accept everything that started with "exit" and ignore the rest.

+1
source

As noted, fgets (3) gives you the final '\ n'. If you use gets (3), you will not get the ending new line. Nothing like a sequence, sez I.

Perl has a manual chomp () function that truncates the final new line, if its current - you can do worse than throw your own:

 #define NUL ((char)0) void chomp( char *s ) { if ( s != null ) { int len = strlen(s) ; if ( len >= 1 && s[len-1] == "\n" ) { s[len-1] = NUL ; } } return ; } 
0
source

Source: https://habr.com/ru/post/1338350/


All Articles