Both functions can be well documented ( fread , fgets ) on the C ++ website. Refer to them for depth and technical difference.
In short, fgets will read until the first new line, the maximum bytes to read immediately or EOF that are ever sent first, while fread will read a certain number of words (where I define a word as a piece of bytes, say, a group of 4 bytes) and stop when this limit is reached or 0 bytes are read (usually this means EOF or error).
If you want to use any function to read before EOF , then it will look like this:
char buffer[ buff_len ]; // ... zero-fill buffer here. while ( fgets( buffer, buff_len, stdin ) != EOF ) { // ... do something with buffer (will be NULL terminated). } while ( fread( buffer, sizeof( buffer[ 0 ] ), sizeof( buffer ) / sizeof( buffer[ 0 ] ), stdin ) != 0 ) { // ... do something with buffer (not necessarily NULL terminated). }
source share