Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz program, który przepisze z istniejącego pliku tekstowego czterdzieści znaków z początku i czterdzieści znaków z końca do nowego pliku.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #define READ_FILE_PATHNAME "01.c"
- #define NEW_FILE_PATHNAME "01_file"
- #define BUFFER_SIZE 40
- void rewrite_file(int read_fd, int write_fd)
- {
- char buffer[BUFFER_SIZE];
- memset(buffer, '\0', BUFFER_SIZE);
- if(read(read_fd, buffer, BUFFER_SIZE-1) == -1) {
- perror("read");
- exit(EXIT_FAILURE);
- }
- if(write(write_fd, buffer, BUFFER_SIZE-1) == -1) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- if(write(write_fd, "\n\n", 2) == -1) {
- perror("write");
- exit(EXIT_FAILURE);
- }
- }
- int main(void)
- {
- int read_fd, write_fd;
- if((write_fd = open(NEW_FILE_PATHNAME, O_CREAT | O_TRUNC | O_WRONLY, 0600)) == -1) {
- perror("open write");
- return EXIT_FAILURE;
- }
- if((read_fd = open(READ_FILE_PATHNAME, O_RDONLY)) == -1) {
- perror("open read");
- return EXIT_FAILURE;
- }
- rewrite_file(read_fd, write_fd);
- lseek(read_fd, -40, SEEK_END);
- rewrite_file(read_fd, write_fd);
- if(close(read_fd) == -1) {
- perror("close read");
- return EXIT_FAILURE;
- }
- if(close(write_fd) == -1) {
- perror("close write");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Advertisement
Add Comment
Please, Sign In to add comment