W. Керриск LINUX API ИСЧЕРПЫВАЮЩЕЕ РУКОВОДСТВО. Задача из главы 5.14 (5.3)
Запустите
$ atomic_append f1 1000000 & atomic_append f1 1000000
Повторите те же действия, ведя запись в другой файл, но на этот раз с указанием аргумента x.
$ atomic_append f2 1000000 x & atomic_append f2 1000000 x
[b]Выведите на экран размеры файлов f1 и f2, воспользовавшись командой ls -l, и объясните разницу между файлами.[/b]
Я значит всё это выполнил и не понял, почему размер файлов разный… Помогите, плиз. [code=C] #include <fcntl.h> // open #include <stdio.h> // printf #include <errno.h> #include <unistd.h> // read #include <string.h> // strcmp #include <stdlib.h> // ERROR_SUCCESS #include «error_functions.h»
int main(int argc, char *argv[]) { int fd; char *buf; #define BUF_SIZE 1024 int len;
if (strcmp(argv[1], "--help") == 0)
usageErr("%s filename bytes [x]", argv[0]);
if (argc == 4 && strcmp(argv[3], "x") == 0) {
fd = open(argv[1], O_WRONLY);
if (fd == -1 && errno != ENOENT)
errExit("open");
else {
fd = open(argv[1], O_WRONLY | O_CREAT, S_IRUSR | S_IWUSR);
if (fd == -1)
errExit("open");
}
}
else {
fd = open(argv[1], O_WRONLY | O_APPEND);
if (fd == -1)
errExit("open");
}
len = atoll(argv[2]);
int zCount= len % 1024;
for (int i = 0; i < len / BUF_SIZE; ++i) {
if (lseek(fd, 0, SEEK_END) == -1)
errExit("lseek");
buf = malloc(BUF_SIZE);
if (buf == NULL) {
errExit("malloc");
}
if (write(fd, buf, BUF_SIZE) == -1) {
errExit("write");
}
free(buf);
buf = NULL;
}
buf = malloc(len%BUF_SIZE);
if (buf == NULL) {
errExit("malloc");
}
if (write(fd, buf, len%BUF_SIZE) == -1) {
errExit("write");
}
free(buf);
exit(EXIT_SUCCESS);
} [/code]






