Advertisement
Guest User

Untitled

a guest
Apr 24th, 2018
73
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.76 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdint.h>
  3. #include <string.h>
  4. #include <unistd.h>
  5. #include <stdlib.h>
  6. #include <fcntl.h>
  7.  
  8.  
  9. void file_create (const char* filename) {
  10.     int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  11.     close(fd);
  12. }
  13.  
  14.  
  15. void file_print(const char* filename) {
  16.     int fd = open(filename, O_RDONLY);
  17.     int cur;
  18.     int rd;
  19.     while ((rd = read(fd, &cur, sizeof(cur))) > 0) {
  20.         printf("%d ", cur);
  21.     }
  22.     printf("\n");
  23.  
  24.     close(fd);
  25. }
  26.  
  27.  
  28. int quantity_of_num(int fd, int num) {
  29.     int res = 0;
  30.     int cur;
  31.     while ((read(fd, &cur, sizeof(cur))) > 0) {
  32.         if (cur == num) {
  33.             ++res;
  34.         }
  35.     }
  36.  
  37.     return res;
  38. }
  39.  
  40.  
  41. void task_02(const char* filename1, const char* filename2) {
  42.     file_print(filename1);
  43.     file_create(filename2);
  44.  
  45.     int fd1 = open(filename1, O_RDONLY);
  46.     int fd2 = open(filename2, O_WRONLY);
  47.  
  48.     int cur;
  49.     while (read(fd1, &cur, sizeof(cur)) > 0) {
  50.         int cur_pos = lseek(fd1, 0, SEEK_CUR);
  51.         lseek(fd1, 0, SEEK_SET);
  52.         int quantity = quantity_of_num(fd1, cur);
  53.         write(fd2, &cur, sizeof(cur));
  54.         write(fd2, &quantity, sizeof(quantity));
  55.         lseek(fd1, cur_pos, SEEK_SET);
  56.     }
  57.  
  58.     close(fd1);
  59.     close(fd2);
  60.  
  61.     file_print(filename2);
  62. }
  63.  
  64.  
  65. void file_write(const char* filename, int* arr, size_t arrsz) {
  66.     int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
  67.     write(fd, arr, arrsz * sizeof(int));
  68.     close(fd);
  69. }
  70.  
  71. int main(int argc, char** argv) {
  72.     int len = strtol(argv[1], NULL, 10);
  73.     int arr[len];
  74.  
  75.     int cur;
  76.     for (size_t i = 0; i != len; ++i) {
  77.         scanf("%d", &cur);
  78.         arr[i] = cur;
  79.     }
  80.  
  81.     file_write(argv[2], arr, len);
  82.  
  83.     task_02(argv[2], argv[3]);
  84. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement