Numerator
Numerator

Reputation: 1409

Input/output problems

#define MAX_COMMAND_LEN 32

char command[MAX_COMMAND_LEN];
while (1) {
    if (fgets(command, MAX_COMMAND_LEN, stdin) == NULL) {
        perror("Error: standard function fgets has failed\n");
        break;
    }

    if (command[strlen(command) -1] != '\n') {
        printf("Error: command length must be less than or equal to 30 characters\n");
        continue;
    }
    else {
         printf("Error: command not found\n");
    }

}
quit();

I have couple of problems which I'm not able to handle:

  1. When I press Enter, it stops the loop and doesn't print the command not found message.
  2. When I enter a command with a size bigger than 30 characters it prints both the command not found and the command length must be less than or equal to 30 characters messages.
  3. When I enter a 64 size command it prints twice the 30-length message.

I believe it divides the input to 30-length segments and input each one of them, how do I overcome it? I tried to flush stdin, it does not work. I want to get rid of the rest of the input. How do I overcome all these problems?

Upvotes: 0

Views: 235

Answers (4)

MYMNeo
MYMNeo

Reputation: 836

For question (i), sorry, I don't know why, because my program gives the correct output.

For question (ii), you give the second argument of fgets is 32, the function will read at most 32 characters including the '\n' and '\0'; what's left is still in the stdin buffer, and when your program continues after printing the error, the fgets will read the leftover characters in the stdin buffer until it reads '\n'.

If you want to flush the stdin, you need fpurge(stdin) function to purge the stdin buffer.

Upvotes: 1

Some programmer dude
Some programmer dude

Reputation: 409384

For your second problem, it's because fgets fetches the 31 (MAX_COMMAND_LEN, minus space for the terminating '\0' character) first characters, you notice it's no newline and the next time around the loop fgets fetches the remaining characters.

Upvotes: 2

Eitan T
Eitan T

Reputation: 32930

When I enter a command with a size bigger than 30 characters it prints both the 'command not found' and the 'command length must be less than or equal to 30 characters' messages.

fgets reads a maximum of MAX_COMMAND_LEN - 1 characters as it leaves room for a '\0'.
This is why for any message of more than 30 character, the first 31 characters that fgets reads don't contain a '\n' and so the 30-length message is displayed.
As the second part of the command has a '\n' in the end, the command not found is also printed.

When I enter a 64 size command it prints twice the 30-length message.

fgets is called 3 times for this command. The first 31-length chunk is read, then the second 31-length chunk is read, and then the remaining characters. Both 31-length chunks don't contain a '\n' character and therefore the 30-length message is displayed twice.

Upvotes: 2

Zaur Nasibov
Zaur Nasibov

Reputation: 22679

You may have misunderstood how fgets actually works:

char * fgets ( char * str, int num, FILE * stream ); Reads characters from stream and stores them as a C string into str until (num-1) characters have been read or either a newline or the End-of-File is reached, whichever comes first. A newline character makes fgets stop reading, but it is considered a valid character and therefore it is included in the string copied to str. A null character is automatically appended in str after the characters read to signal the end of the C string.

So,

  1. when you press "Enter" - it inputs newline and that is not exceptional case in your code.
  2. when you input a string bigger than 30 characters fget reads it for multiple times.
  3. same with the 60 characters - newline is NOT the last character of the string, thus you get an error twice.

Upvotes: 1

Related Questions