diff options
[SP/P01] Solved exercises from first practice (again)
1 files changed, 32 insertions, 0 deletions
| -rw-r--r-- | Year 3/System programming/Practice01/ex5-cut.c | 32 |
diff --git a/Year 3/System programming/Practice01/ex5-cut.c b/Year 3/System programming/Practice01/ex5-cut.c new file mode 100644 index 0000000..e1d6bf2 --- /dev/null +++ b/Year 3/System programming/Practice01/ex5-cut.c @@ -0,0 +1,32 @@ +#include <fcntl.h> // open, close, O_RDONLY +#include <stdio.h> // printf, scanf, perror +#include <unistd.h> // read + +// print characters from column m to n (1 indexed) for each row +int main(int argc, char* argv[]) { + int f = open(argv[1], O_RDONLY); + if (f < 0) { + perror("open"); + return 1; + } + + int m, n; + scanf("%d %d", &m, &n); + + int col = 1; + char buf; + while (read(f, &buf, 1)) { + if (buf == '\n') { + col = 1; + printf("\n"); + continue; + } + + if (m <= col && col <= n) { + printf("%c", buf); + } + col++; + } + + close(f); +} |
