linux - EOF and PAGESIZE in mmap in C -


i have code read file using mmap , print using printf. file has 10 lines, , contains nos 0-9 on each line.

my questions are:
1. why code doesn't terminate on eof ? i.e. why doesn't stop @ while (data[i]!=eof) ?
2. when run while (data[i]!=eof), program terminates @ data[10567] ? page size 4096 bytes. 10567 bytes have significance ?

edit: not looking alternative using fscanf, fgets.

thanks!

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", &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<=sbuf.st_size)  45         {  46                 printf("data[%d]=%c\n", i, data[i]);  47                 i++;  48         }  50         return 0;  51 } 

output:

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 output here,             // goes till data[10567] if use `while (data[i]!=eof)` 

eof not stored in files. there's no point comparing byte file eof. if use mmap, opposed getchar or equivalent, need stat file find out how big is.

note getc, fgetc , getchar return int. quoting manpage (or posix standard), these functions return next byte "as unsigned char cast int, or eof on end of file or error." value of eof must such cannot confused "an unsigned char cast int"; typically, -1. is possible random (signed) char equal -1, test data[i]!=eof may turn out true scan through uninitialized memory, if don't segfault before hit random byte.

in unix, text files not terminated nuls either. in short, should try reference bytes know inside file, based on file's size.


Comments

Popular posts from this blog

apache - Remove .php and add trailing slash in url using htaccess not loading css -

javascript - jQuery show full size image on click -