Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <unistd.h>
- #include <fcntl.h>
- #include <limits.h>
- #include <string.h>
- #define MAX(a, b) a > b ? a : b
- #define MIN(a, b) a < b ? a : b
- int getFD(const char *const path, int flags, int perm) {
- int fd = open(path, flags, perm);
- if (fd < 0) {
- perror("Open err: ");
- exit(1);
- }
- return fd;
- }
- void closeFD(int fd) {
- if (close(fd) < 0) {
- perror("Close err: ");
- exit(1);
- }
- }
- int main(int argc, char *argv[]) {
- if (argc != 3) {
- fprintf(stderr, "Wrong usage!\n");
- exit(1);
- }
- int fd1 = getFD(argv[1], O_RDONLY, 0);
- int nr;
- int max = INT_MIN, min = INT_MAX;
- int cnt = 0, sum = 0;
- while (read(fd1, &nr, sizeof(int)) > 0) {
- min = MIN(min, nr);
- max = MAX(max, nr);
- sum += nr;
- cnt++;
- }
- int fd2 = getFD(argv[2], O_RDWR | O_CREAT, S_IWUSR | S_IRUSR);
- char template[50] = "";
- sprintf(template, " Min: %d\n Max: %d\n Mean: %lf\n", min, max, 1.0 * sum/cnt);
- size_t templateLen = strlen(template);
- if (write(fd2, template, templateLen) == -1) {
- perror("Write err: ");
- exit(1);
- }
- closeFD(fd1);
- closeFD(fd2);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement