Advertisement
Guest User

Untitled

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