Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Napisz program, który przeczyta i wypisze na ekran plik tekstowy o dowolnej wielkości. Nazwę pliku należy przekazywać jako argument wywołania programu. W programie nie wolno korzystać z funkcji printf() ani pokrewnych.
- */
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <string.h>
- #define BUFFER_SIZE 128
- const char *argument_number_error = "Niepoprawna liczba argumentow!\n";
- int main(int argc, char *argv[])
- {
- if(argc != 2) {
- write(STDIN_FILENO, argument_number_error, strlen(argument_number_error));
- return EXIT_FAILURE;
- }
- int fd;
- char buffer[BUFFER_SIZE];
- int result;
- if((fd = open(argv[1], O_RDONLY | 0600)) == -1) {
- perror("open");
- return EXIT_FAILURE;
- }
- for(;;) {
- memset(buffer, '\0', BUFFER_SIZE);
- result = read(fd, buffer, BUFFER_SIZE-1);
- if(result == -1) {
- perror("read");
- return EXIT_FAILURE;
- }
- else if(result == 0)
- break;
- else
- write(STDIN_FILENO, buffer, strlen(buffer));
- }
- write(STDIN_FILENO, "\n", 1);
- if(close(fd)) {
- perror("close");
- return EXIT_FAILURE;
- }
- return EXIT_SUCCESS;
- }
Add Comment
Please, Sign In to add comment