Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdint.h>
- #include <string.h>
- #include <unistd.h>
- #include <stdlib.h>
- #include <fcntl.h>
- void file_create (const char* filename) {
- int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- close(fd);
- }
- void file_print(const char* filename) {
- int fd = open(filename, O_RDONLY);
- int cur;
- int rd;
- while ((rd = read(fd, &cur, sizeof(cur))) > 0) {
- printf("%d ", cur);
- }
- printf("\n");
- close(fd);
- }
- int quantity_of_num(int fd, int num) {
- int res = 0;
- int cur;
- while ((read(fd, &cur, sizeof(cur))) > 0) {
- if (cur == num) {
- ++res;
- }
- }
- return res;
- }
- void task_02(const char* filename1, const char* filename2) {
- file_print(filename1);
- file_create(filename2);
- int fd1 = open(filename1, O_RDONLY);
- int fd2 = open(filename2, O_WRONLY);
- int cur;
- while (read(fd1, &cur, sizeof(cur)) > 0) {
- int cur_pos = lseek(fd1, 0, SEEK_CUR);
- lseek(fd1, 0, SEEK_SET);
- int quantity = quantity_of_num(fd1, cur);
- write(fd2, &cur, sizeof(cur));
- write(fd2, &quantity, sizeof(quantity));
- lseek(fd1, cur_pos, SEEK_SET);
- }
- close(fd1);
- close(fd2);
- file_print(filename2);
- }
- void file_write(const char* filename, int* arr, size_t arrsz) {
- int fd = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0600);
- write(fd, arr, arrsz * sizeof(int));
- close(fd);
- }
- int main() {
- int len;
- scanf("%d", &len);
- int arr[len];
- int cur;
- for (size_t i = 0; i != len; ++i) {
- scanf("%d", &cur);
- arr[i] = cur;
- }
- char filename1[20];
- scanf("%s", filename1);
- char filename2[20];
- scanf("%s", filename2);
- file_write(filename1, arr, len);
- task_02(filename1, filename2);
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement