diff --git a/doc/trivial_example.c b/doc/trivial_example.c
index abeba1c2da6d72128cbe628d05d3cb14048c7292..9cf435b4a9a00002090299b96ef4c07745205311 100644
--- a/doc/trivial_example.c
+++ b/doc/trivial_example.c
@@ -113,14 +113,25 @@ int main(int argc, char **argv)
       int i;
       unsigned char pcm_bytes[MAX_FRAME_SIZE*CHANNELS*2];
       int frame_size;
+      size_t samples;
 
       /* Read a 16 bits/sample audio frame. */
-      fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
-      if (feof(fin))
+      samples = fread(pcm_bytes, sizeof(short)*CHANNELS, FRAME_SIZE, fin);
+
+      /* For simplicity, only read whole frames. In a real application,
+       * we'd pad the final partial frame with zeroes, record the exact
+       * duration, and trim the decoded audio to match.
+       */
+      if (samples != FRAME_SIZE)
+      {
          break;
+      }
+
       /* Convert from little-endian ordering. */
       for (i=0;i<CHANNELS*FRAME_SIZE;i++)
+      {
          in[i]=pcm_bytes[2*i+1]<<8|pcm_bytes[2*i];
+      }
 
       /* Encode the frame. */
       nbBytes = opus_encode(encoder, in, FRAME_SIZE, cbits, MAX_PACKET_SIZE);