Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <sys/types.h>
- #include <sys/stat.h>
- #include <fcntl.h>
- #include <unistd.h>
- #include <ctype.h>
- #include <string.h>
- #define BLOCK_SIZE (1 << 12)
- #define AUX_SIZE 50
- int openFile(const char path[], int flags, mode_t mode) {
- int fd = open(path, flags, mode);
- if (fd < 0) {
- perror("");
- exit(-1);
- }
- return fd;
- }
- void closeFile(int fd) {
- if (close(fd) == -1) {
- perror("");
- exit(-2);
- }
- }
- int getLowerCount(int n, const char buff[]) {
- int cnt = 0;
- for (int i = 0; i < n; i++) {
- if (islower(buff[i])) {
- cnt++;
- }
- }
- return cnt;
- }
- int main(int argc, char *argv[]) {
- if (argc != 3) {
- fprintf(stderr, "Wrong usage!\n");
- exit(-1);
- }
- int fdin = openFile(argv[1], O_RDONLY, 0);
- int fdout = openFile(argv[2], O_WRONLY | O_CREAT | O_TRUNC, S_IRWXU);
- char buff[BLOCK_SIZE];
- int cntLower = 0, readed;
- while ((readed = read(fdin, buff, BLOCK_SIZE)) > 0) {
- cntLower += getLowerCount(readed, buff);
- char aux[AUX_SIZE] = {};
- for (int i = 0; i < readed; i++) {
- if (buff[i] >= 97 && buff[i] <= 122) {
- snprintf(aux, AUX_SIZE, "%c", buff[i]);
- } else {
- snprintf(aux, AUX_SIZE, "(%02X)", buff[i]);
- }
- if (write(fdout, aux, strlen(aux)) == -1) {
- perror("");
- exit(-3);
- }
- }
- }
- if (write(fdout, "\n", 1) == -1) {
- perror("");
- exit(-3);
- }
- {
- char aux[AUX_SIZE];
- snprintf(aux, AUX_SIZE, "Lower letters: %d\n", cntLower);
- if (write(fdout, aux, strlen(aux)) == -1) {
- perror("");
- exit(-3);
- }
- }
- closeFile(fdin);
- closeFile(fdout);
- return 0;
- }//dd if=/dev/urandom of=in.txt bs=1000 count=100
Add Comment
Please, Sign In to add comment