Reputation: 903
#define MAX_BUFF_SIZE 64
char input[MAX_BUFF_SIZE];
int inSize = read(0, input, MAX_BUFF_SIZE);
if(inSize <= 0 || inSize > MAX_BUFF_SIZE){
printf("An error occurred in the read.\n");
exit(-1);
}
I'm writing a program that would prompt the user for input and this input has a maximum size of 64 characters. If the user inputs more than 64 characters, the program should exit.
I believe I'm using read()
correctly but it never ever causes any errors/exceptions even if I pass in > 64 chars. What am I doing wrong here?
Is there anyway that I can check to see if the user inputs more than 64 chars with read()?
Upvotes: 0
Views: 1899
Reputation:
read()
will never read more bytes than the amount you allow it. If you tell it to read at most 64 bytes, it will read at most 64 bytes, even if there's more data available. The yet unread bytes are available for further reading (I assume the input
is STDIN_FILENO
, in which case they're simply left in the stdin buffer).
Upvotes: 2