Загрузка данных



#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <fcntl.h>
#include <unistd.h>

int main(int argc, char *argv[]) {
    if (argc != 3) {
        return 1;
    }

    if (access(argv[2], F_OK) == 0) {
        char answer;

        printf("Файл существует. Перезаписать? (y/n): ");
        scanf(" %c", &answer);

        if (answer != 'y' && answer != 'Y') {
            return 0;
        }
    }

    int in = open(argv[1], O_RDONLY);

    if (in == -1) {
        perror("open");
        return 1;
    }

    int out = open(argv[2], O_WRONLY | O_CREAT | O_TRUNC, 0666);

    if (out == -1) {
        perror("open");
        close(in);
        return 1;
    }

    int16_t x;
    int n = 1;
    ssize_t r;

    while ((r = read(in, &x, sizeof(x))) == sizeof(x)) {
        char line[100];

        int len = snprintf(line, sizeof(line),
            "%-10d|%-10d|%-10o|%-10x\n",
            n, x, (unsigned short)x, (unsigned short)x);

        if (write(out, line, len) != len) {
            perror("write");
            close(in);
            close(out);
            return 1;
        }

        n++;
    }

    if (r != 0) {
        fprintf(stderr, "Ошибка чтения или неполное число\n");
        close(in);
        close(out);
        return 1;
    }

    close(in);
    close(out);

    return 0;
}