Advertisement
ihitklif

Файлы

Oct 26th, 2016
102
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.29 KB | None | 0 0
  1. #include <unistd.h>
  2. #include <fcntl.h>
  3. #include <stdlib.h>
  4. #include <stdio.h>
  5.  
  6. enum {size = 50 };
  7.  
  8. //количество вхождений символа "с"
  9. int fcount(const char *path, char c)
  10. {
  11.     int fd = open(path, O_RDONLY);
  12.     if (fd < 0) return -1;
  13.     //место под массив, куда считываем
  14.     char a[size];
  15.     //считываем по 50 элементов
  16.     int n = 0, count = 0;
  17.     while ((n = read (fd, a, size)) > 0)
  18.     {
  19.         int i = 0;
  20.         for(; i < n; i++)
  21.         {
  22.             if (a[i] == c) count++;
  23.         }
  24.     }
  25.  
  26.     close(fd);
  27.     return count;
  28.  
  29. }
  30.  
  31. //переименовать файл
  32. int rename1(char *from, char *to )
  33. {
  34.     unlink( to ); /*удалитьфайлto */
  35.     if( link( from, to ) < 0 ) /* связать */
  36.         return (-1);
  37.     unlink( from ); /* стереть старое имя */
  38.     return 0; /* OK */
  39. }
  40.  
  41. //вернуть размер файла
  42. off_t size_file (const char *path)
  43. {
  44.     int fd = open(path, O_RDONLY);
  45.     if (fd == -1 ) return -1;
  46.     off_t s = lseek (fd, 0, SEEK_END);
  47.     close(fd);
  48.     return(s);
  49. }
  50.  
  51.  
  52.  
  53. int main (void)
  54. {
  55.     //int a = rename1 ("a.rtf", "b.rtf");
  56.     off_t b = size_file ("b.rtf");
  57.     long long l = b;
  58.     printf("%ll\n", l);
  59. };
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement