Advertisement
Guest User

Untitled

a guest
Feb 16th, 2012
146
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <fcntl.h>
  4. #include <sys/mman.h>
  5.  
  6. #define MAX_FILE_SIZE 65535
  7.  
  8. int main ()
  9. {
  10.     int fd = open("~/input.txt",O_RDONLY,0);
  11.     void *bytes;
  12.     void *byteptr;
  13.     int i = 0;
  14.  
  15.     if (fp == -1)
  16.     {
  17.         printf("File not found!\n");
  18.         exit(EXIT_FAILURE);
  19.     }
  20.  
  21.     bytes = mmap(NULL,MAX_FILE_SIZE,PROT_READ,MAP_PRIVATE,fd,0);
  22.  
  23.     while ((char)*byteptr != EOF)
  24.     {
  25.         char *line = malloc(256*sizeof(char));
  26.         char *lineptr = line;
  27.         int line_size = 0;
  28.         while (*lineptr++ = (char) *byteptr++ != '\n' && (char) *byteptr != EOF)
  29.             line_size++;
  30.         *lineptr = '\0';
  31.         printf("Line size: %s\nLine: %s\n",line_size,line);
  32.         i++;
  33.     free(line);
  34.     }
  35.  
  36.     munmap(bytes, MAX_FILE_SIZE);  
  37.     close(fd);
  38.     printf("Total lines: %i\n", i);
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement