Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- 6 #include <stdio.h>
- 7 #include <fcntl.h>
- 8 #include <sys/mman.h>
- 9 #include <sys/stat.h>
- 10 #include <unistd.h>
- 11 #include "mmap.h"
- 12
- 13 int main (int argc, char* const argv[]) {
- 14 int fd;
- 15 void* file_memory;
- 16 int integer;
- 17
- 18 int num = atoi(argv[2]);
- 19 /* open the file */
- 20 fd = open (argv[1], O_RDWR, S_IRUSR | S_IWUSR);
- 21
- 22
- 23 /* create the memory-mapping */
- 24 file_memory = mmap (NULL, FILESIZE, PROT_READ | PROT_WRITE,
- 25 MAP_SHARED, fd, 0);
- 26
- 27 void* curr_memory_map = file_memory;
- 28 close (fd);
- 29
- 30 for(int i = 0 ; i < num ;i++){
- 31 /* read and print the integer */
- 32 sscanf (curr_memory_map, "%d", &integer);
- 33 int incrementation = printf ("file contains: %d\n", integer);
- 34
- 35 curr_memory_map +=incrementation;
- 36 char* curr_value = curr_memory_map;
- 37
- 38 if (!curr_value){
- 39 break;
- 40 }
- 41
- 42 }
- 43 /* release the memory */
- 44 munmap (file_memory, FILESIZE);
- 45
- 46 return 0;
- 47 }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement