Advertisement
Guest User

Untitled

a guest
Nov 13th, 2018
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.09 KB | None | 0 0
  1. //-----------------------------------------------------------------------
  2. // NAME: Dimitur Stoilov
  3. // CLASS: XIa
  4. // NUMBER: 8
  5. // PROBLEM: #1
  6. // FILE NAME: files.c
  7. // FILE PURPOSE:
  8. // Prints the first 10 lines of a file
  9. // ...
  10. //-----------------------------------------------------------------------
  11. #include<stdio.h>
  12. #include<sys/types.h>
  13. #include<sys/stat.h>
  14. #include<fcntl.h>
  15. #include<unistd.h>
  16.  
  17. int main(){
  18.     char buff;
  19.     int count = 0;
  20.     int w_result;
  21.     int a=0;
  22.    
  23.     int fd = open("a.txt", O_APPEND);
  24.  
  25.     if(fd == -1){
  26.         write(1, "error\n", 6);
  27.     }
  28.  
  29.     int result = 1;
  30.    
  31.     while(result > 0){
  32.         if(buff=='\n'){
  33.             count++;
  34.             if(count==10){
  35.                 break;
  36.             }
  37.  
  38.         }
  39.         result = read(fd, &buff, 1);
  40.         int written = 0;
  41.  
  42.         while(written < result){
  43.             w_result = write(STDOUT_FILENO, &buff, result - written);
  44.             if(w_result == -1){
  45.                 write(1, "error\n", 6);
  46.                 return -1;
  47.             }
  48.             written += w_result;
  49.            
  50.         }
  51.        
  52.     }
  53.     if(result == -1){
  54.         write(1, "error\n", 6);
  55.     }
  56.    
  57.    
  58.     if(close(fd) == -1){
  59.         write(1, "error\n", 6);
  60.     }
  61. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement