Summary Files Commits Branches @commit @diff @stats
path: root / Year 3 / System programming / Practice01 / ex5-cut.c
blob: e1d6bf2ec7531d781c25327ec5447f8ef1e58706 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
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);
}