Advertisement
Guest User

Untitled

a guest
Dec 1st, 2015
64
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.67 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <sys/types.h>
  4. #include <sys/stat.h>
  5. #include <fcntl.h>
  6. #include <unistd.h>
  7.  
  8. int main()
  9. {
  10. int lines_to_print = 0;
  11. int i = 0;
  12. int num_lines = 0;
  13. char buffer[500];
  14. int first_file = open("first.txt", O_RDONLY);
  15. if(first_file < 0)
  16. {
  17. perror("Error in opening the first file");
  18. return 0;
  19. }
  20. while (read(first_file, buffer, 1))
  21. {
  22. for (i = 0; buffer[i]; i++)
  23. { if (buffer[i] == '\n')
  24. {
  25. num_lines++;
  26. }
  27. }
  28. }
  29. lines_to_print = num_lines - 10;
  30. for (i = lines_to_print; i <= num_lines; i++)
  31. {
  32. write(STDOUT_FILENO, &buffer[i], 10);
  33. }
  34.  
  35. close(first_file);
  36. return 0;
  37. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement