title: EOF and PAGESIZE in mmap in C
body: <p>I have this code to read a file using <code>mmap</code> and print it using <code>printf</code>. The file has 10 lines, and contains nos <code>0-9</code> on each line.</p>
  
  <p><strong>My questions are:</strong><br>
   1. Why my code doesn't terminate on <code>EOF</code> ? i.e. why doesn't it stop at <code>while (data[i]!=EOF)</code> ?<br>
   2. When I run it with <code>while (data[i]!=EOF)</code>, the program always terminates at <code>data[10567]</code> ? where as the page size is <code>4096 bytes</code>. Does <code>10567</code> bytes have any significance ?</p>
  
  <p><strong>Edit:</strong> I am not looking for alternative like using <code>fscanf</code>, <code>fgets</code>. </p>
  
  <p>Thanks!</p>
  
  <p><strong>Code:</strong>  </p>
  
  <pre><code> 10 int main(int argc, char *argv[])
   11 {
   12         FILE *ifp, *ofp;
   13         int pagesize, fd, i=0;
   14         char *data;
   15         struct stat sbuf;
   16
   18         if ((ifp = fopen("/home/t/workspace/lin", "r"))==NULL)
   19         {
   20                 fprintf(stderr, "Can't open input file\n");
   21                 exit(1);
   22         }
   28         fd  = fileno(ifp);
   29         if (stat("/home/t/workspace/lin", &amp;sbuf) == -1)
   30         {
   31                 perror("stat");
   32                 exit(1);
   33         }
   34         pagesize = getpagesize();
   35         printf("page size: %d\n", pagesize);
   36         printf("file size: %d\n", sbuf.st_size);
   37         if((data = mmap((caddr_t)0, sbuf.st_size, PROT_READ, MAP_SHARED, fd, 0)) == (caddr_t)(-1))
   38         {
   39                 perror("mmap");
   40                 exit(1);
   41         }
   43         //while (data[i]!=EOF)
   44         while (i&lt;=sbuf.st_size)
   45         {
   46                 printf("data[%d]=%c\n", i, data[i]);
   47                 i++;
   48         }
   50         return 0;
   51 }
  </code></pre>
  
  <p><strong>Output:</strong></p>
  
  <pre><code>page size: 4096
  file size: 21
  data[0]=0
  data[1]=
  
  data[2]=1
  data[3]=
  
  data[4]=2
  data[5]=
  
  data[6]=3
  data[7]=
  
  data[8]=4
  data[9]=
  
  . . . .
  
  data[18]=9
  data[19]=
  
  data[20]=
  
  data[21]=  // truncated my output here, 
             // it goes till data[10567] if I use `while (data[i]!=EOF)`
  </code></pre>